2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
5 * Parts were written/modified by:
6 * Nico Golde <nico@ngolde.de>
7 * Andreas Krennmair <ak@synflood.at>
9 * This file is part of mutt-ng, see http://www.muttng.org/.
10 * It's licensed under the GNU General Public License,
11 * please see the file GPL in the top level source directory.
19 #include "mutt_curses.h"
21 #include "mutt_menu.h"
33 #include "imap_private.h"
36 #include "mutt_crypt.h"
50 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
52 #define IsAttach(x) (x && (x)->bdy)
53 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
54 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
55 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
56 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
58 static const char *Not_available_in_this_menu =
59 N_("Not available in this menu.");
60 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
61 static const char *Function_not_permitted_in_attach_message_mode =
62 N_("Function not permitted in attach-message mode.");
64 #define CHECK_MODE(x) if (!(x)) \
67 mutt_error _(Not_available_in_this_menu); \
71 #define CHECK_READONLY if (Context->readonly) \
74 mutt_error _(Mailbox_is_read_only); \
78 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
81 mutt_error _(Function_not_permitted_in_attach_message_mode); \
90 struct q_class_t *next, *prev;
91 struct q_class_t *down, *up;
106 struct syntax_t *syntax;
107 struct syntax_t *search;
108 struct q_class_t *quote;
111 #define ANSI_OFF (1<<0)
112 #define ANSI_BLINK (1<<1)
113 #define ANSI_BOLD (1<<2)
114 #define ANSI_UNDERLINE (1<<3)
115 #define ANSI_REVERSE (1<<4)
116 #define ANSI_COLOR (1<<5)
118 typedef struct _ansi_attr {
125 static short InHelp = 0;
127 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
128 static struct resize {
135 #define NumSigLines 4
137 static int check_sig (const char *s, struct line_t *info, int n)
141 while (n > 0 && count <= NumSigLines) {
142 if (info[n].type != MT_COLOR_SIGNATURE)
151 if (count > NumSigLines) {
152 /* check for a blank line */
166 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
167 int special, ansi_attr * a)
169 int def_color; /* color without syntax hilight */
170 int color; /* final color */
171 static int last_color; /* last color set */
172 int search = 0, i, m;
175 last_color = -1; /* force attrset() */
177 if (lineInfo[n].continuation) {
178 if (!cnt && option (OPTMARKERS)) {
179 SETCOLOR (MT_COLOR_MARKERS);
181 last_color = ColorDefs[MT_COLOR_MARKERS];
183 m = (lineInfo[n].syntax)[0].first;
184 cnt += (lineInfo[n].syntax)[0].last;
188 if (!(flags & M_SHOWCOLOR))
189 def_color = ColorDefs[MT_COLOR_NORMAL];
190 else if (lineInfo[m].type == MT_COLOR_HEADER)
191 def_color = (lineInfo[m].syntax)[0].color;
193 def_color = ColorDefs[lineInfo[m].type];
195 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
196 struct q_class_t *class = lineInfo[m].quote;
199 def_color = class->color;
201 while (class && class->length > cnt) {
202 def_color = class->color;
209 if (flags & M_SHOWCOLOR) {
210 for (i = 0; i < lineInfo[m].chunks; i++) {
211 /* we assume the chunks are sorted */
212 if (cnt > (lineInfo[m].syntax)[i].last)
214 if (cnt < (lineInfo[m].syntax)[i].first)
216 if (cnt != (lineInfo[m].syntax)[i].last) {
217 color = (lineInfo[m].syntax)[i].color;
220 /* don't break here, as cnt might be
221 * in the next chunk as well */
225 if (flags & M_SEARCH) {
226 for (i = 0; i < lineInfo[m].search_cnt; i++) {
227 if (cnt > (lineInfo[m].search)[i].last)
229 if (cnt < (lineInfo[m].search)[i].first)
231 if (cnt != (lineInfo[m].search)[i].last) {
232 color = ColorDefs[MT_COLOR_SEARCH];
239 /* handle "special" bold & underlined characters */
240 if (special || a->attr) {
242 if ((a->attr & ANSI_COLOR)) {
244 a->pair = mutt_alloc_color (a->fg, a->bg);
246 if (a->attr & ANSI_BOLD)
251 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
252 if (ColorDefs[MT_COLOR_BOLD] && !search)
253 color = ColorDefs[MT_COLOR_BOLD];
257 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
258 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
259 color = ColorDefs[MT_COLOR_UNDERLINE];
261 color ^= A_UNDERLINE;
263 else if (a->attr & ANSI_REVERSE) {
266 else if (a->attr & ANSI_BLINK) {
269 else if (a->attr & ANSI_OFF) {
274 if (color != last_color) {
280 static void append_line (struct line_t *lineInfo, int n, int cnt)
284 lineInfo[n + 1].type = lineInfo[n].type;
285 (lineInfo[n + 1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
286 lineInfo[n + 1].continuation = 1;
288 /* find the real start of the line */
289 for (m = n; m >= 0; m--)
290 if (lineInfo[m].continuation == 0)
293 (lineInfo[n + 1].syntax)[0].first = m;
294 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
295 cnt + (lineInfo[n].syntax)[0].last : cnt;
298 static void new_class_color (struct q_class_t *class, int *q_level)
300 class->index = (*q_level)++;
301 class->color = ColorQuote[class->index % ColorQuoteUsed];
305 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
306 int index, int *q_level)
308 struct q_class_t *q_list;
311 new_class->index = -1;
314 if (q_list->index >= index) {
316 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
319 q_list = q_list->down;
320 else if (q_list->next)
321 q_list = q_list->next;
323 while (!q_list->next) {
329 q_list = q_list->next;
333 new_class->index = index;
334 new_class->color = ColorQuote[index % ColorQuoteUsed];
338 static void cleanup_quote (struct q_class_t **QuoteList)
340 struct q_class_t *ptr;
343 if ((*QuoteList)->down)
344 cleanup_quote (&((*QuoteList)->down));
345 ptr = (*QuoteList)->next;
346 if ((*QuoteList)->prefix)
347 FREE (&(*QuoteList)->prefix);
355 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
356 const char *qptr, int length,
357 int *force_redraw, int *q_level)
359 struct q_class_t *q_list = *QuoteList;
360 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
362 int offset, tail_lng;
365 if (ColorQuoteUsed <= 1) {
366 /* not much point in classifying quotes... */
368 if (*QuoteList == NULL) {
369 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
370 class->color = ColorQuote[0];
376 /* Did I mention how much I like emulating Lisp in C? */
378 /* classify quoting prefix */
380 if (length <= q_list->length) {
381 /* case 1: check the top level nodes */
383 if (safe_strncmp (qptr, q_list->prefix, length) == 0) {
384 if (length == q_list->length)
385 return q_list; /* same prefix: return the current class */
387 /* found shorter prefix */
389 /* add a node above q_list */
391 (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
392 tmp->prefix = (char *) safe_calloc (1, length + 1);
393 strncpy (tmp->prefix, qptr, length);
394 tmp->length = length;
396 /* replace q_list by tmp in the top level list */
398 tmp->next = q_list->next;
399 q_list->next->prev = tmp;
402 tmp->prev = q_list->prev;
403 q_list->prev->next = tmp;
406 /* make q_list a child of tmp */
410 /* q_list has no siblings for now */
414 /* update the root if necessary */
415 if (q_list == *QuoteList)
418 index = q_list->index;
420 /* tmp should be the return class too */
423 /* next class to test; if tmp is a shorter prefix for another
424 * node, that node can only be in the top level list, so don't
425 * go down after this point
430 /* found another branch for which tmp is a shorter prefix */
432 /* save the next sibling for later */
435 /* unlink q_list from the top level list */
437 q_list->next->prev = q_list->prev;
439 q_list->prev->next = q_list->next;
441 /* at this point, we have a tmp->down; link q_list to it */
443 /* sibling order is important here, q_list should be linked last */
451 index = q_list->index;
453 /* next class to test; as above, we shouldn't go down */
457 /* we found a shorter prefix, so certain quotes have changed classes */
462 /* shorter, but not a substring of the current class: try next */
463 q_list = q_list->next;
468 /* case 2: try subclassing the current top level node */
470 /* tmp != NULL means we already found a shorter prefix at case 1 */
472 && safe_strncmp (qptr, q_list->prefix, q_list->length) == 0) {
473 /* ok, it's a subclass somewhere on this branch */
476 offset = q_list->length;
478 q_list = q_list->down;
479 tail_lng = length - offset;
480 tail_qptr = (char *) qptr + offset;
483 if (length <= q_list->length) {
484 if (safe_strncmp (tail_qptr, (q_list->prefix) + offset, tail_lng)
486 /* same prefix: return the current class */
487 if (length == q_list->length)
490 /* found shorter common prefix */
492 /* add a node above q_list */
493 tmp = (struct q_class_t *) safe_calloc (1,
496 tmp->prefix = (char *) safe_calloc (1, length + 1);
497 strncpy (tmp->prefix, qptr, length);
498 tmp->length = length;
500 /* replace q_list by tmp */
502 tmp->next = q_list->next;
503 q_list->next->prev = tmp;
506 tmp->prev = q_list->prev;
507 q_list->prev->next = tmp;
510 /* make q_list a child of tmp */
512 tmp->up = q_list->up;
514 if (tmp->up->down == q_list)
517 /* q_list has no siblings */
521 index = q_list->index;
523 /* tmp should be the return class too */
526 /* next class to test */
530 /* found another branch for which tmp is a shorter prefix */
532 /* save the next sibling for later */
535 /* unlink q_list from the top level list */
537 q_list->next->prev = q_list->prev;
539 q_list->prev->next = q_list->next;
541 /* at this point, we have a tmp->down; link q_list to it */
550 index = q_list->index;
552 /* next class to test */
556 /* we found a shorter prefix, so we need a redraw */
561 q_list = q_list->next;
566 /* longer than the current prefix: try subclassing it */
568 && safe_strncmp (tail_qptr, (q_list->prefix) + offset,
569 q_list->length - offset) == 0) {
570 /* still a subclass: go down one level */
572 offset = q_list->length;
574 q_list = q_list->down;
575 tail_lng = length - offset;
576 tail_qptr = (char *) qptr + offset;
581 /* nope, try the next prefix */
582 q_list = q_list->next;
588 /* still not found so far: add it as a sibling to the current node */
591 (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
592 tmp->prefix = (char *) safe_calloc (1, length + 1);
593 strncpy (tmp->prefix, qptr, length);
594 tmp->length = length;
597 tmp->next = ptr->down;
598 ptr->down->prev = tmp;
603 new_class_color (tmp, q_level);
609 shift_class_colors (*QuoteList, tmp, index, q_level);
615 /* nope, try the next prefix */
616 q_list = q_list->next;
623 /* not found so far: add it as a top level class */
624 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
625 class->prefix = (char *) safe_calloc (1, length + 1);
626 strncpy (class->prefix, qptr, length);
627 class->length = length;
628 new_class_color (class, q_level);
631 class->next = *QuoteList;
632 (*QuoteList)->prev = class;
638 shift_class_colors (*QuoteList, tmp, index, q_level);
643 static int check_attachment_marker (char *);
646 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
647 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
650 COLOR_LINE *color_line;
651 regmatch_t pmatch[1], smatch[1];
652 int found, offset, null_rx, i;
654 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
656 lineInfo[n].type = MT_COLOR_NORMAL;
657 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
658 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
659 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
662 lineInfo[n].type = MT_COLOR_HDEFAULT;
663 color_line = ColorHdrList;
665 if (REGEXEC (&color_line->rx, buf) == 0) {
666 lineInfo[n].type = MT_COLOR_HEADER;
667 lineInfo[n].syntax[0].color = color_line->pair;
670 color_line = color_line->next;
674 else if (safe_strncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
675 lineInfo[n].type = MT_COLOR_NORMAL;
677 else if (safe_strncmp ("[-- ", buf, 4) == 0)
678 lineInfo[n].type = MT_COLOR_ATTACHMENT;
680 else if (check_attachment_marker ((char *) raw) == 0)
681 lineInfo[n].type = MT_COLOR_ATTACHMENT;
683 else if (safe_strcmp ("-- \n", buf) == 0
684 || safe_strcmp ("-- \r\n", buf) == 0) {
687 lineInfo[n].type = MT_COLOR_SIGNATURE;
688 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
689 (lineInfo[i].type == MT_COLOR_NORMAL ||
690 lineInfo[i].type == MT_COLOR_QUOTED ||
691 lineInfo[i].type == MT_COLOR_HEADER)) {
693 if (lineInfo[i].chunks) {
694 lineInfo[i].chunks = 0;
695 safe_realloc (&(lineInfo[n].syntax), sizeof (struct syntax_t));
697 lineInfo[i++].type = MT_COLOR_SIGNATURE;
700 else if (check_sig (buf, lineInfo, n - 1) == 0)
701 lineInfo[n].type = MT_COLOR_SIGNATURE;
702 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
703 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
704 if (smatch[0].rm_so > 0) {
707 /* hack to avoid making an extra copy of buf */
708 c = buf[smatch[0].rm_so];
709 buf[smatch[0].rm_so] = 0;
711 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
712 if (q_classify && lineInfo[n].quote == NULL)
713 lineInfo[n].quote = classify_quote (QuoteList,
714 buf + pmatch[0].rm_so,
716 pmatch[0].rm_so, force_redraw,
718 lineInfo[n].type = MT_COLOR_QUOTED;
721 lineInfo[n].type = MT_COLOR_NORMAL;
723 buf[smatch[0].rm_so] = c;
726 lineInfo[n].type = MT_COLOR_NORMAL;
729 if (q_classify && lineInfo[n].quote == NULL)
730 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
731 pmatch[0].rm_eo - pmatch[0].rm_so,
732 force_redraw, q_level);
733 lineInfo[n].type = MT_COLOR_QUOTED;
737 lineInfo[n].type = MT_COLOR_NORMAL;
740 if (lineInfo[n].type == MT_COLOR_NORMAL ||
741 lineInfo[n].type == MT_COLOR_QUOTED) {
745 lineInfo[n].chunks = 0;
752 color_line = ColorBodyList;
754 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
755 (offset ? REG_NOTBOL : 0)) == 0) {
756 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
758 if (++(lineInfo[n].chunks) > 1)
759 safe_realloc (&(lineInfo[n].syntax),
760 (lineInfo[n].chunks) *
761 sizeof (struct syntax_t));
763 i = lineInfo[n].chunks - 1;
764 pmatch[0].rm_so += offset;
765 pmatch[0].rm_eo += offset;
767 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
768 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
769 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
770 (lineInfo[n].syntax)[i].color = color_line->pair;
771 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
772 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
778 null_rx = 1; /* empty regexp; don't add it, but keep looking */
780 color_line = color_line->next;
784 offset++; /* avoid degenerate cases */
786 offset = (lineInfo[n].syntax)[i].last;
787 } while (found || null_rx);
791 static int is_ansi (unsigned char *buf)
793 while (*buf && (isdigit (*buf) || *buf == ';'))
795 return (*buf == 'm');
798 static int check_attachment_marker (char *p)
800 char *q = AttachmentMarker;
802 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
803 return (int) (*p - *q);
806 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
810 while (isdigit (buf[x]) || buf[x] == ';')
813 /* Character Attributes */
814 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
818 mutt_free_color (a->fg, a->bg);
824 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
825 a->attr |= ANSI_BOLD;
828 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
829 a->attr |= ANSI_UNDERLINE;
832 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
833 a->attr |= ANSI_BLINK;
836 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
837 a->attr |= ANSI_REVERSE;
840 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
843 mutt_free_color (a->fg, a->bg);
849 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
852 mutt_free_color (a->fg, a->bg);
855 a->attr |= ANSI_COLOR;
856 a->fg = buf[pos + 1] - '0';
859 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
862 mutt_free_color (a->fg, a->bg);
865 a->attr |= ANSI_COLOR;
866 a->bg = buf[pos + 1] - '0';
870 while (pos < x && buf[pos] != ';')
881 fill_buffer (FILE * f, long *last_pos, long offset, unsigned char *buf,
882 unsigned char *fmt, size_t blen, int *buf_ready)
885 static int b_read = 0;
887 if (*buf_ready == 0) {
889 if (offset != *last_pos)
890 fseek (f, offset, 0);
891 if (fgets ((char *) buf, blen - 1, f) == NULL) {
895 *last_pos = ftell (f);
896 b_read = (int) (*last_pos - offset);
899 /* copy "buf" to "fmt", but without bold and underline controls */
902 if (*p == '\010' && (p > buf)) {
903 if (*(p + 1) == '_') /* underline */
905 else if (*(p + 1)) { /* bold or overstrike */
906 *(fmt - 1) = *(p + 1);
912 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
913 while (*p++ != 'm') /* skip ANSI sequence */
916 else if (*p == '\033' && *(p + 1) == ']'
917 && check_attachment_marker ((char *) p) == 0) {
918 dprint (2, (debugfile, "fill_buffer: Seen attachment marker.\n"));
919 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
936 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
937 int flags, ansi_attr * pa, int cnt,
938 int *pspace, int *pvch, int *pcol, int *pspecial)
940 int space = -1; /* index of the last space or TAB */
941 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
942 int ch, vch, k, last_special = -1, special = 0, t;
946 int wrap_cols = COLS;
948 if (!(flags & (M_SHOWFLAT)))
949 wrap_cols -= WrapMargin;
950 wrap_cols -= SidebarWidth;
955 /* FIXME: this should come from lineInfo */
956 memset (&mbstate, 0, sizeof (mbstate));
958 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
959 /* Handle ANSI sequences */
960 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
961 is_ansi (buf + ch + 2))
962 ch = grok_ansi (buf, ch + 2, pa) + 1;
964 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
965 check_attachment_marker ((char *) buf + ch) == 0) {
966 while (buf[ch++] != '\a')
971 /* is anything left to do? */
975 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
976 if (k == -2 || k == -1) {
977 dprint (1, (debugfile, "%s:%d: mbrtowc returned %d; errno = %d.\n",
978 __FILE__, __LINE__, k, errno));
979 if (col + 4 > wrap_cols)
983 printw ("\\%03o", buf[ch]);
990 /* Handle backspace */
997 while ((wc1 = 0, mbstate1 = mbstate,
999 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
1000 &mbstate1), k1 - k > 0 && wc1 == '\b')
1002 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1003 &mbstate1), k2 > 0 && IsWPrint (wc1))) {
1005 special |= (wc == '_' && special & A_UNDERLINE)
1006 ? A_UNDERLINE : A_BOLD;
1008 else if (wc == '_' || wc1 == '_') {
1009 special |= A_UNDERLINE;
1010 wc = (wc1 == '_') ? wc : wc1;
1013 /* special = 0; / * overstrike: nothing to do! */
1023 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1024 special || last_special || pa->attr)) {
1025 resolve_color (*lineInfo, n, vch, flags, special, pa);
1026 last_special = special;
1029 if (IsWPrint (wc)) {
1033 if (col + t > wrap_cols)
1039 else if (wc == '\n')
1041 else if (wc == '\t') {
1047 for (; col < t; col++)
1052 else if (wc < 0x20 || wc == 0x7f) {
1053 if (col + 2 > wrap_cols)
1057 printw ("^%c", ('@' + wc) & 0x7f);
1059 else if (wc < 0x100) {
1060 if (col + 4 > wrap_cols)
1064 printw ("\\%03o", wc);
1067 if (col + 1 > wrap_cols)
1071 addch (replacement_char ());
1077 *pspecial = special;
1083 * flags M_SHOWFLAT, show characters (used for displaying help)
1084 * M_SHOWCOLOR, show characters in color
1085 * otherwise don't show characters
1086 * M_HIDE, don't show quoted text
1087 * M_SEARCH, resolve search patterns
1088 * M_TYPES, compute line's type
1089 * M_PAGER_NSKIP, keeps leading whitespace
1090 * M_PAGER_MARKER, eventually show markers
1093 * -1 EOF was reached
1094 * 0 normal exit, line was not displayed
1095 * >0 normal exit, line was displayed
1099 display_line (FILE * f, long *last_pos, struct line_t **lineInfo, int n,
1100 int *last, int *max, int flags, struct q_class_t **QuoteList,
1101 int *q_level, int *force_redraw, regex_t * SearchRE)
1103 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1104 unsigned char *buf_ptr = buf;
1105 int ch, vch, col, cnt, b_read;
1106 int buf_ready = 0, change_last = 0;
1111 ansi_attr a = { 0, 0, 0, -1 };
1112 regmatch_t pmatch[1];
1119 if (*last == *max) {
1120 safe_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1121 for (ch = *last; ch < *max; ch++) {
1122 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1123 (*lineInfo)[ch].type = -1;
1124 (*lineInfo)[ch].search_cnt = -1;
1125 (*lineInfo)[ch].syntax = safe_malloc (sizeof (struct syntax_t));
1126 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1131 /* only do color hiliting if we are viewing a message */
1132 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1133 if ((*lineInfo)[n].type == -1) {
1134 /* determine the line class */
1136 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1143 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1144 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1146 /* avoid race condition for continuation lines when scrolling up */
1148 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1150 (*lineInfo)[m].type = (*lineInfo)[n].type;
1153 /* this also prevents searching through the hidden lines */
1154 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1155 flags = 0; /* M_NOSHOW */
1158 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1159 * don't want to compute it every time M_TYPES is set, since this
1160 * would slow down the "bottom" function unacceptably. A compromise
1161 * solution is hence to call regexec() again, just to find out the
1162 * length of the quote prefix.
1164 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1165 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1168 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1174 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1175 (*lineInfo)[n].quote = classify_quote (QuoteList,
1176 (char *) fmt + pmatch[0].rm_so,
1177 pmatch[0].rm_eo - pmatch[0].rm_so,
1178 force_redraw, q_level);
1181 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1182 && (*lineInfo)[n].search_cnt == -1) {
1184 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1192 (*lineInfo)[n].search_cnt = 0;
1194 (SearchRE, (char *) fmt + offset, 1, pmatch,
1195 (offset ? REG_NOTBOL : 0)) == 0) {
1196 if (++((*lineInfo)[n].search_cnt) > 1)
1197 safe_realloc (&((*lineInfo)[n].search),
1198 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1200 (*lineInfo)[n].search = safe_malloc (sizeof (struct syntax_t));
1201 pmatch[0].rm_so += offset;
1202 pmatch[0].rm_eo += offset;
1203 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1205 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1208 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1209 offset++; /* avoid degenerate cases */
1211 offset = pmatch[0].rm_eo;
1217 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1218 /* we've already scanned this line, so just exit */
1221 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1222 /* no need to try to display this line... */
1223 return (1); /* fake display */
1226 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1227 sizeof (buf), &buf_ready)) < 0) {
1233 /* now chose a good place to break the line */
1235 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1237 buf_ptr = buf + cnt;
1239 /* move the break point only if smart_wrap is set */
1240 if (option (OPTWRAP)) {
1242 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1243 && buf[cnt] != '\r') {
1245 /* skip trailing blanks */
1246 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1251 buf_ptr = buf + cnt; /* a very long word... */
1253 if (!(flags & M_PAGER_NSKIP))
1254 /* skip leading blanks on the next line too */
1255 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1259 if (*buf_ptr == '\r')
1261 if (*buf_ptr == '\n')
1264 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1265 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1266 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1268 /* if we don't need to display the line we are done */
1269 if (!(flags & M_SHOW))
1272 /* display the line */
1273 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1275 /* avoid a bug in ncurses... */
1276 #ifndef USE_SLANG_CURSES
1278 SETCOLOR (MT_COLOR_NORMAL);
1283 /* end the last color pattern (needed by S-Lang) */
1284 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1285 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1288 * Fill the blank space at the end of the line with the prevailing color.
1289 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1290 * to make sure to reset the color *after* that
1292 if (flags & M_SHOWCOLOR) {
1293 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1294 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1295 def_color = ((*lineInfo)[m].syntax)[0].color;
1297 def_color = ColorDefs[(*lineInfo)[m].type];
1299 attrset (def_color);
1301 bkgdset (def_color | ' ');
1305 /* ncurses always wraps lines when you get to the right side of the
1306 * screen, but S-Lang seems to only wrap if the next character is *not*
1309 #ifndef USE_SLANG_CURSES
1315 * reset the color back to normal. This *must* come after the
1316 * addch('\n'), otherwise the color for this line will not be
1317 * filled to the right margin.
1319 if (flags & M_SHOWCOLOR) {
1320 SETCOLOR (MT_COLOR_NORMAL);
1321 BKGDSET (MT_COLOR_NORMAL);
1324 /* build a return code */
1325 if (!(flags & M_SHOW))
1331 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1333 while (cur > 0 && nlines > 0) {
1335 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1342 static struct mapping_t PagerHelp[] = {
1343 {N_("Exit"), OP_EXIT},
1344 {N_("PrevPg"), OP_PREV_PAGE},
1345 {N_("NextPg"), OP_NEXT_PAGE},
1348 static struct mapping_t PagerHelpExtra[] = {
1349 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1350 {N_("Del"), OP_DELETE},
1351 {N_("Reply"), OP_REPLY},
1352 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1357 static struct mapping_t PagerNewsHelpExtra[] = {
1358 {N_("Post"), OP_POST},
1359 {N_("Followup"), OP_FOLLOWUP},
1360 {N_("Del"), OP_DELETE},
1361 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1368 /* This pager is actually not so simple as it once was. It now operates in
1369 two modes: one for viewing messages and the other for viewing help. These
1370 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1371 is there so that we can do operations on the current message without the
1372 need to pop back out to the main-menu. */
1374 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1376 static char searchbuf[STRING];
1377 char buffer[LONG_STRING];
1378 char helpstr[SHORT_STRING * 2];
1379 char tmphelp[SHORT_STRING * 2];
1380 int maxLine, lastLine = 0;
1381 struct line_t *lineInfo;
1382 struct q_class_t *QuoteList = NULL;
1383 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1384 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1386 int redraw = REDRAW_FULL;
1388 long last_pos = 0, last_offset = 0;
1389 int old_smart_wrap, old_markers;
1392 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1393 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1395 int bodyoffset = 1; /* offset of first line of real text */
1396 int statusoffset = 0; /* offset for the status bar */
1397 int helpoffset = LINES - 2; /* offset for the help bar. */
1398 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1400 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1401 int indexoffset = 0; /* offset for the PI */
1402 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1403 int indicator = indexlen / 3; /* the indicator line of the PI */
1404 int old_PagerIndexLines; /* some people want to resize it
1405 * while inside the pager... */
1411 if (!(flags & M_SHOWCOLOR))
1412 flags |= M_SHOWFLAT;
1414 if ((fp = fopen (fname, "r")) == NULL) {
1415 mutt_perror (fname);
1419 if (stat (fname, &sb) != 0) {
1420 mutt_perror (fname);
1426 /* Initialize variables */
1428 if (IsHeader (extra) && !extra->hdr->read) {
1429 Context->msgnotreadyet = extra->hdr->msgno;
1430 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1433 lineInfo = safe_malloc (sizeof (struct line_t) * (maxLine = LINES));
1434 for (i = 0; i < maxLine; i++) {
1435 memset (&lineInfo[i], 0, sizeof (struct line_t));
1436 lineInfo[i].type = -1;
1437 lineInfo[i].search_cnt = -1;
1438 lineInfo[i].syntax = safe_malloc (sizeof (struct syntax_t));
1439 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1442 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1443 if (IsHeader (extra)) {
1444 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1445 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1448 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1451 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1454 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1455 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1456 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1466 if (redraw & REDRAW_FULL) {
1467 SETCOLOR (MT_COLOR_NORMAL);
1468 /* clear() doesn't optimize screen redraws */
1472 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1473 indexlen = Context->vcount + 1;
1475 indexlen = PagerIndexLines;
1477 indicator = indexlen / 3;
1479 if (option (OPTSTATUSONTOP)) {
1481 statusoffset = IsHeader (extra) ? indexlen : 0;
1482 bodyoffset = statusoffset + 1;
1483 helpoffset = LINES - 2;
1484 bodylen = helpoffset - bodyoffset;
1485 if (!option (OPTHELP))
1491 statusoffset = LINES - 2;
1492 if (!option (OPTHELP))
1494 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1495 bodylen = statusoffset - bodyoffset;
1498 if (option (OPTHELP)) {
1499 SETCOLOR (MT_COLOR_STATUS);
1500 move (helpoffset, 0);
1501 mutt_paddstr (COLS, helpstr);
1502 SETCOLOR (MT_COLOR_NORMAL);
1505 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1506 if (Resize != NULL) {
1507 if ((SearchCompiled = Resize->SearchCompiled)) {
1509 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1510 SearchFlag = M_SEARCH;
1511 SearchBack = Resize->SearchBack;
1513 lines = Resize->line;
1514 redraw |= REDRAW_SIGWINCH;
1520 if (IsHeader (extra) && PagerIndexLines) {
1521 if (index == NULL) {
1522 /* only allocate the space if/when we need the index.
1523 Initialise the menu as per the main index */
1524 index = mutt_new_menu ();
1525 index->menu = MENU_MAIN;
1526 index->make_entry = index_make_entry;
1527 index->color = index_color;
1528 index->max = Context->vcount;
1529 index->current = extra->hdr->virtual;
1532 SETCOLOR (MT_COLOR_NORMAL);
1533 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1535 index->pagelen = indexlen - 1;
1537 /* some fudge to work out where abouts the indicator should go */
1538 if (index->current - indicator < 0)
1540 else if (index->max - index->current < index->pagelen - indicator)
1541 index->top = index->max - index->pagelen;
1543 index->top = index->current - indicator;
1545 menu_redraw_index (index);
1548 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1552 if (redraw & REDRAW_SIGWINCH) {
1555 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1556 has_types | SearchFlag, &QuoteList, &q_level,
1557 &force_redraw, &SearchRE) == 0) {
1558 if (!lineInfo[i].continuation && ++j == lines) {
1563 redraw |= REDRAW_SIDEBAR;
1567 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1569 move (bodyoffset, SidebarWidth);
1570 curline = oldtopline = topline;
1574 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1575 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1577 (flags & M_DISPLAYFLAGS) | hideQuoted |
1578 SearchFlag, &QuoteList, &q_level, &force_redraw,
1582 move (lines + bodyoffset, SidebarWidth);
1583 redraw |= REDRAW_SIDEBAR;
1585 last_offset = lineInfo[curline].offset;
1586 } while (force_redraw);
1588 SETCOLOR (MT_COLOR_TILDE);
1589 BKGDSET (MT_COLOR_TILDE);
1590 while (lines < bodylen) {
1592 if (option (OPTTILDE))
1596 move (lines + bodyoffset, SidebarWidth);
1598 /* We are going to update the pager status bar, so it isn't
1599 * necessary to reset to normal color now. */
1601 redraw |= REDRAW_STATUS; /* need to update the % seen */
1604 if (redraw & REDRAW_STATUS) {
1605 /* print out the pager status bar */
1606 SETCOLOR (MT_COLOR_STATUS);
1607 BKGDSET (MT_COLOR_STATUS);
1608 CLEARLINE_WIN (statusoffset);
1609 if (IsHeader (extra)) {
1610 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1611 size_t l2 = sizeof (buffer);
1613 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1614 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1616 else if (IsMsgAttach (extra)) {
1617 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1618 size_t l2 = sizeof (buffer);
1620 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1621 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1623 if (option(OPTSTATUSONTOP)) {
1626 /*move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (InHelp?(LINES-2):(indexlen - 1))),
1627 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);*/
1628 mutt_paddstr (COLS - 10 - (option(OPTSTATUSONTOP)?0:SidebarWidth), IsHeader (extra)
1629 || IsMsgAttach (extra) ? buffer : banner);
1632 if (last_pos < sb.st_size - 1)
1633 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1635 addstr (topline == 0 ? "all)" : "end)");
1636 BKGDSET (MT_COLOR_NORMAL);
1637 SETCOLOR (MT_COLOR_NORMAL);
1640 if (redraw & REDRAW_SIDEBAR)
1641 sidebar_draw (MENU_PAGER);
1643 if ((redraw & REDRAW_INDEX) && index) {
1644 /* redraw the pager_index indicator, because the
1645 * flags for this message might have changed. */
1646 menu_redraw_current (index);
1647 sidebar_draw (MENU_PAGER);
1648 /* print out the index status bar */
1649 menu_status_line (buffer, sizeof (buffer), index, NONULL (Status));
1650 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)),
1651 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);
1652 SETCOLOR (MT_COLOR_STATUS);
1653 mutt_paddstr (COLS - (option (OPTSTATUSONTOP) ? 0 : SidebarWidth),
1655 SETCOLOR (MT_COLOR_NORMAL);
1657 /* if we're not using the index, update every time */
1659 sidebar_draw (MENU_PAGER);
1663 move (statusoffset, COLS - 1);
1665 ch = km_dokey (MENU_PAGER);
1667 mutt_clear_error ();
1674 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1675 else if (SigWinch) {
1676 mutt_resize_screen ();
1678 /* Store current position. */
1680 for (i = 0; i <= topline; i++)
1681 if (!lineInfo[i].continuation)
1684 if (flags & M_PAGER_RETWINCH) {
1685 Resize = safe_malloc (sizeof (struct resize));
1687 Resize->line = lines;
1688 Resize->SearchCompiled = SearchCompiled;
1689 Resize->SearchBack = SearchBack;
1692 rc = OP_REFORMAT_WINCH;
1695 for (i = 0; i < maxLine; i++) {
1696 lineInfo[i].offset = 0;
1697 lineInfo[i].type = -1;
1698 lineInfo[i].continuation = 0;
1699 lineInfo[i].chunks = 0;
1700 lineInfo[i].search_cnt = -1;
1701 lineInfo[i].quote = NULL;
1703 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
1704 if (SearchCompiled && lineInfo[i].search)
1705 FREE (&(lineInfo[i].search));
1711 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1716 clearok (stdscr, TRUE); /*force complete redraw */
1720 else if (ch == -1) {
1734 if (lineInfo[curline].offset < sb.st_size - 1) {
1735 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1737 else if (option (OPTPAGERSTOP)) {
1738 /* emulate "less -q" and don't go on to the next message. */
1739 mutt_error _("Bottom of message is shown.");
1742 /* end of the current message, so display the next message. */
1743 rc = OP_MAIN_NEXT_UNDELETED;
1751 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1754 mutt_error _("Top of message is shown.");
1758 if (lineInfo[curline].offset < sb.st_size - 1) {
1761 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1767 mutt_error _("Bottom of message is shown.");
1772 topline = upNLines (1, lineInfo, topline, hideQuoted);
1774 mutt_error _("Top of message is shown.");
1781 mutt_error _("Top of message is shown.");
1786 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1788 mutt_error _("Top of message is shown.");
1792 if (lineInfo[curline].offset < sb.st_size - 1) {
1793 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1795 else if (option (OPTPAGERSTOP)) {
1796 /* emulate "less -q" and don't go on to the next message. */
1797 mutt_error _("Bottom of message is shown.");
1800 /* end of the current message, so display the next message. */
1801 rc = OP_MAIN_NEXT_UNDELETED;
1806 case OP_SEARCH_NEXT:
1807 case OP_SEARCH_OPPOSITE:
1808 if (SearchCompiled) {
1810 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1811 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1812 /* searching forward */
1813 for (i = topline + 1; i < lastLine; i++) {
1814 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1815 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1822 mutt_error _("Not found.");
1825 /* searching backward */
1826 for (i = topline - 1; i >= 0; i--) {
1827 if ((!hideQuoted || (has_types &&
1828 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1829 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1836 mutt_error _("Not found.");
1839 if (lineInfo[topline].search_cnt > 0)
1840 SearchFlag = M_SEARCH;
1844 /* no previous search pattern, so fall through to search */
1847 case OP_SEARCH_REVERSE:
1848 strfcpy (buffer, searchbuf, sizeof (buffer));
1849 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1850 _("Search: ")), buffer, sizeof (buffer),
1854 if (!strcmp (buffer, searchbuf)) {
1855 if (SearchCompiled) {
1856 /* do an implicit search-next */
1857 if (ch == OP_SEARCH)
1858 ch = OP_SEARCH_NEXT;
1860 ch = OP_SEARCH_OPPOSITE;
1869 strfcpy (searchbuf, buffer, sizeof (searchbuf));
1871 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1872 if (ch == OP_SEARCH)
1874 else if (ch == OP_SEARCH_REVERSE)
1877 if (SearchCompiled) {
1878 regfree (&SearchRE);
1879 for (i = 0; i < lastLine; i++) {
1880 if (lineInfo[i].search)
1881 FREE (&(lineInfo[i].search));
1882 lineInfo[i].search_cnt = -1;
1887 REGCOMP (&SearchRE, searchbuf,
1888 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1889 regerror (err, &SearchRE, buffer, sizeof (buffer));
1890 mutt_error ("%s", buffer);
1891 regfree (&SearchRE);
1892 for (i = 0; i < maxLine; i++) {
1894 if (lineInfo[i].search)
1895 FREE (&(lineInfo[i].search));
1896 lineInfo[i].search_cnt = -1;
1903 /* update the search pointers */
1905 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1906 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1907 &QuoteList, &q_level,
1908 &force_redraw, &SearchRE) == 0) {
1910 redraw |= REDRAW_SIDEBAR;
1914 /* searching forward */
1915 for (i = topline; i < lastLine; i++) {
1916 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1917 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1925 /* searching backward */
1926 for (i = topline; i >= 0; i--) {
1927 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1928 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1936 if (lineInfo[topline].search_cnt == 0) {
1938 mutt_error _("Not found.");
1941 SearchFlag = M_SEARCH;
1943 redraw = REDRAW_BODY;
1946 case OP_SEARCH_TOGGLE:
1947 if (SearchCompiled) {
1948 SearchFlag ^= M_SEARCH;
1949 redraw = REDRAW_BODY;
1954 /* don't let the user enter the help-menu from the help screen! */
1957 mutt_help (MENU_PAGER);
1958 redraw = REDRAW_FULL;
1962 mutt_error _("Help is currently being shown.");
1965 case OP_PAGER_HIDE_QUOTED:
1967 hideQuoted ^= M_HIDE;
1968 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1969 topline = upNLines (1, lineInfo, topline, hideQuoted);
1971 redraw = REDRAW_BODY;
1975 case OP_PAGER_SKIP_QUOTED:
1978 int new_topline = topline;
1980 while ((new_topline < lastLine ||
1981 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1982 new_topline, &lastLine,
1983 &maxLine, M_TYPES, &QuoteList,
1984 &q_level, &force_redraw,
1986 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
1987 redraw |= REDRAW_SIDEBAR;
1992 mutt_error _("No more quoted text.");
1997 while ((new_topline < lastLine ||
1998 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1999 new_topline, &lastLine,
2000 &maxLine, M_TYPES, &QuoteList,
2001 &q_level, &force_redraw,
2003 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2005 redraw |= REDRAW_SIDEBAR;
2009 mutt_error _("No more unquoted text after quoted text.");
2013 topline = new_topline;
2017 case OP_PAGER_BOTTOM: /* move to the end of the file */
2018 if (lineInfo[curline].offset < sb.st_size - 1) {
2020 /* make sure the types are defined to the end of file */
2021 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2022 &maxLine, has_types,
2023 &QuoteList, &q_level, &force_redraw,
2026 redraw |= REDRAW_SIDEBAR;
2028 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2031 mutt_error _("Bottom of message is shown.");
2035 clearok (stdscr, TRUE);
2036 redraw = REDRAW_FULL;
2040 km_error_key (MENU_PAGER);
2043 /* --------------------------------------------------------------------
2044 * The following are operations on the current message rather than
2045 * adjusting the view of the message.
2048 case OP_BOUNCE_MESSAGE:
2049 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2051 if (IsMsgAttach (extra))
2052 mutt_attach_bounce (extra->fp, extra->hdr,
2053 extra->idx, extra->idxlen, extra->bdy);
2055 ci_bounce_message (extra->hdr, &redraw);
2059 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2061 if (IsMsgAttach (extra))
2062 mutt_attach_resend (extra->fp, extra->hdr,
2063 extra->idx, extra->idxlen, extra->bdy);
2065 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2066 redraw = REDRAW_FULL;
2069 case OP_CHECK_TRADITIONAL:
2070 CHECK_MODE (IsHeader (extra));
2071 if (!(WithCrypto & APPLICATION_PGP))
2073 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2075 rc = OP_CHECK_TRADITIONAL;
2079 case OP_CREATE_ALIAS:
2080 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2081 if (IsMsgAttach (extra))
2082 mutt_create_alias (extra->bdy->hdr->env, NULL);
2084 mutt_create_alias (extra->hdr->env, NULL);
2085 MAYBE_REDRAW (redraw);
2088 case OP_PURGE_MESSAGE:
2090 CHECK_MODE (IsHeader (extra));
2093 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2095 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2096 mutt_set_flag (Context, extra->hdr, M_PURGED,
2097 ch != OP_PURGE_MESSAGE ? 0 : 1);
2098 if (option (OPTDELETEUNTAG))
2099 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2100 redraw = REDRAW_STATUS | REDRAW_INDEX;
2101 if (option (OPTRESOLVE)) {
2103 rc = OP_MAIN_NEXT_UNDELETED;
2107 case OP_DELETE_THREAD:
2108 case OP_DELETE_SUBTHREAD:
2109 CHECK_MODE (IsHeader (extra));
2112 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2114 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2115 ch == OP_DELETE_THREAD ? 0 : 1);
2118 if (option (OPTDELETEUNTAG))
2119 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2120 ch == OP_DELETE_THREAD ? 0 : 1);
2121 if (option (OPTRESOLVE)) {
2122 rc = OP_MAIN_NEXT_UNDELETED;
2126 if (!option (OPTRESOLVE) && PagerIndexLines)
2127 redraw = REDRAW_FULL;
2129 redraw = REDRAW_STATUS | REDRAW_INDEX;
2133 case OP_DISPLAY_ADDRESS:
2134 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2135 if (IsMsgAttach (extra))
2136 mutt_display_address (extra->bdy->hdr->env);
2138 mutt_display_address (extra->hdr->env);
2141 case OP_ENTER_COMMAND:
2142 old_smart_wrap = option (OPTWRAP);
2143 old_markers = option (OPTMARKERS);
2144 old_PagerIndexLines = PagerIndexLines;
2146 CurrentMenu = MENU_PAGER;
2147 mutt_enter_command ();
2149 if (option (OPTNEEDRESORT)) {
2150 unset_option (OPTNEEDRESORT);
2151 CHECK_MODE (IsHeader (extra));
2152 set_option (OPTNEEDRESORT);
2155 if (old_PagerIndexLines != PagerIndexLines) {
2157 mutt_menuDestroy (&index);
2161 if (option (OPTWRAP) != old_smart_wrap ||
2162 option (OPTMARKERS) != old_markers) {
2163 if (flags & M_PAGER_RETWINCH) {
2165 rc = OP_REFORMAT_WINCH;
2169 /* count the real lines above */
2171 for (i = 0; i <= topline; i++) {
2172 if (!lineInfo[i].continuation)
2176 /* we need to restart the whole thing */
2177 for (i = 0; i < maxLine; i++) {
2178 lineInfo[i].offset = 0;
2179 lineInfo[i].type = -1;
2180 lineInfo[i].continuation = 0;
2181 lineInfo[i].chunks = 0;
2182 lineInfo[i].search_cnt = -1;
2183 lineInfo[i].quote = NULL;
2185 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2186 if (SearchCompiled && lineInfo[i].search)
2187 FREE (&(lineInfo[i].search));
2190 if (SearchCompiled) {
2191 regfree (&SearchRE);
2196 /* try to keep the old position */
2199 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2200 &lastLine, &maxLine,
2201 (has_types ? M_TYPES : 0),
2202 &QuoteList, &q_level, &force_redraw,
2204 redraw |= REDRAW_SIDEBAR;
2205 if (!lineInfo[topline].continuation)
2214 if (option (OPTFORCEREDRAWPAGER))
2215 redraw = REDRAW_FULL;
2216 unset_option (OPTFORCEREDRAWINDEX);
2217 unset_option (OPTFORCEREDRAWPAGER);
2220 case OP_FLAG_MESSAGE:
2221 CHECK_MODE (IsHeader (extra));
2224 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2226 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2227 redraw = REDRAW_STATUS | REDRAW_INDEX;
2228 if (option (OPTRESOLVE)) {
2230 rc = OP_MAIN_NEXT_UNDELETED;
2235 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2236 if (IsAttach (extra))
2237 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2239 mutt_pipe_message (extra->hdr);
2240 MAYBE_REDRAW (redraw);
2244 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2245 if (IsAttach (extra))
2246 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2248 mutt_print_message (extra->hdr);
2252 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2254 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2255 redraw = REDRAW_FULL;
2260 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2262 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2263 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2264 query_quadoption (OPT_TOMODERATED,
2266 ("Posting to this group not allowed, may be moderated. Continue?"))
2269 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2270 redraw = REDRAW_FULL;
2273 case OP_FORWARD_TO_GROUP:
2274 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2276 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2277 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2278 query_quadoption (OPT_TOMODERATED,
2280 ("Posting to this group not allowed, may be moderated. Continue?"))
2283 if (IsMsgAttach (extra))
2284 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2285 extra->idxlen, extra->bdy, SENDNEWS);
2287 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2289 redraw = REDRAW_FULL;
2293 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2296 if (IsMsgAttach (extra))
2297 followup_to = extra->bdy->hdr->env->followup_to;
2299 followup_to = extra->hdr->env->followup_to;
2301 if (!followup_to || safe_strcasecmp (followup_to, "poster") ||
2302 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2303 _("Reply by mail as poster prefers?")) != M_YES) {
2304 if (extra->ctx && extra->ctx->magic == M_NNTP
2305 && !((NNTP_DATA *) extra->ctx->data)->allowed
2306 && query_quadoption (OPT_TOMODERATED,
2308 ("Posting to this group not allowed, may be moderated. Continue?"))
2311 if (IsMsgAttach (extra))
2312 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2313 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2315 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2316 extra->ctx, extra->hdr);
2317 redraw = REDRAW_FULL;
2323 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2325 if (IsMsgAttach (extra))
2326 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2327 extra->idxlen, extra->bdy, SENDREPLY);
2329 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2330 redraw = REDRAW_FULL;
2333 case OP_RECALL_MESSAGE:
2334 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2336 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2337 redraw = REDRAW_FULL;
2340 case OP_GROUP_REPLY:
2341 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2343 if (IsMsgAttach (extra))
2344 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2345 extra->idxlen, extra->bdy,
2346 SENDREPLY | SENDGROUPREPLY);
2348 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2350 redraw = REDRAW_FULL;
2354 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2356 if (IsMsgAttach (extra))
2357 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2358 extra->idxlen, extra->bdy,
2359 SENDREPLY | SENDLISTREPLY);
2361 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2363 redraw = REDRAW_FULL;
2366 case OP_FORWARD_MESSAGE:
2367 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2369 if (IsMsgAttach (extra))
2370 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2371 extra->idxlen, extra->bdy, 0);
2373 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2374 redraw = REDRAW_FULL;
2377 case OP_DECRYPT_SAVE:
2384 if (IsAttach (extra)) {
2385 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2390 case OP_COPY_MESSAGE:
2391 case OP_DECODE_SAVE:
2392 case OP_DECODE_COPY:
2393 case OP_DECRYPT_COPY:
2394 if (!WithCrypto && ch == OP_DECRYPT_COPY) {
2398 CHECK_MODE (IsHeader (extra));
2399 if (mutt_save_message (extra->hdr,
2400 (ch == OP_DECRYPT_SAVE) ||
2401 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2402 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2403 (ch == OP_DECRYPT_SAVE)
2404 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2405 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2406 || ch == OP_DECRYPT_SAVE)) {
2407 if (option (OPTRESOLVE)) {
2409 rc = OP_MAIN_NEXT_UNDELETED;
2412 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2414 MAYBE_REDRAW (redraw);
2417 case OP_SHELL_ESCAPE:
2418 mutt_shell_escape ();
2419 MAYBE_REDRAW (redraw);
2423 CHECK_MODE (IsHeader (extra));
2424 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2426 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2427 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2428 ? NULL : Context->last_tag);
2430 redraw = REDRAW_STATUS | REDRAW_INDEX;
2431 if (option (OPTRESOLVE)) {
2438 CHECK_MODE (IsHeader (extra));
2441 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2443 if (extra->hdr->read || extra->hdr->old)
2444 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2446 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2448 Context->msgnotreadyet = -1;
2449 redraw = REDRAW_STATUS | REDRAW_INDEX;
2450 if (option (OPTRESOLVE)) {
2452 rc = OP_MAIN_NEXT_UNDELETED;
2457 CHECK_MODE (IsHeader (extra));
2460 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2462 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2463 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2464 redraw = REDRAW_STATUS | REDRAW_INDEX;
2465 if (option (OPTRESOLVE)) {
2471 case OP_UNDELETE_THREAD:
2472 case OP_UNDELETE_SUBTHREAD:
2473 CHECK_MODE (IsHeader (extra));
2476 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2478 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2479 ch == OP_UNDELETE_THREAD ? 0 : 1)
2480 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2481 ch == OP_UNDELETE_THREAD ? 0 : 1);
2484 if (option (OPTRESOLVE)) {
2485 rc = (ch == OP_DELETE_THREAD) ?
2486 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2490 if (!option (OPTRESOLVE) && PagerIndexLines)
2491 redraw = REDRAW_FULL;
2493 redraw = REDRAW_STATUS | REDRAW_INDEX;
2503 redraw |= REDRAW_SIDEBAR;
2506 case OP_VIEW_ATTACHMENTS:
2507 if (flags & M_PAGER_ATTACHMENT) {
2509 rc = OP_ATTACH_COLLAPSE;
2512 CHECK_MODE (IsHeader (extra));
2513 mutt_view_attachments (extra->hdr);
2514 if (extra->hdr->attach_del)
2515 Context->changed = 1;
2516 redraw = REDRAW_FULL;
2521 if (!(WithCrypto & APPLICATION_PGP)) {
2525 CHECK_MODE (IsHeader (extra));
2527 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2528 redraw = REDRAW_FULL;
2532 case OP_FORGET_PASSPHRASE:
2533 crypt_forget_passphrase ();
2536 case OP_EXTRACT_KEYS:
2541 CHECK_MODE (IsHeader (extra));
2542 crypt_extract_keys_from_messages (extra->hdr);
2543 redraw = REDRAW_FULL;
2546 case OP_SIDEBAR_SCROLL_UP:
2547 case OP_SIDEBAR_SCROLL_DOWN:
2548 case OP_SIDEBAR_NEXT:
2549 case OP_SIDEBAR_NEXT_NEW:
2550 case OP_SIDEBAR_PREV:
2551 case OP_SIDEBAR_PREV_NEW:
2552 sidebar_scroll (ch, MENU_PAGER);
2561 if (IsHeader (extra))
2562 Context->msgnotreadyet = -1;
2564 cleanup_quote (&QuoteList);
2566 for (i = 0; i < maxLine; i++) {
2567 FREE (&(lineInfo[i].syntax));
2568 if (SearchCompiled && lineInfo[i].search)
2569 FREE (&(lineInfo[i].search));
2571 if (SearchCompiled) {
2572 regfree (&SearchRE);
2577 mutt_menuDestroy (&index);
2578 return (rc != -1 ? rc : 0);