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.
14 #include <lib-lib/lib-lib.h>
16 #include <lib-ui/curses.h>
17 #include <lib-ui/enter.h>
18 #include <lib-ui/menu.h>
19 #include <lib-ui/sidebar.h>
20 #include <lib-mx/mx.h>
29 #include "recvattach.h"
33 #include <imap/imap_private.h>
35 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
37 #define IsAttach(x) (x && (x)->bdy)
38 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
39 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
40 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
41 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
42 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
44 /* hack to return to position when returning from index to same message */
45 static int TopLine = 0;
46 static HEADER *OldHdr = NULL;
48 #define CHECK_MODE(x) \
51 mutt_error _("Not available in this menu."); \
55 #define CHECK_READONLY \
56 if (Context->readonly) { \
58 mutt_error _("Mailbox is read-only."); \
62 #define CHECK_ATTACH \
63 if (option(OPTATTACHMSG)) { \
65 mutt_error _("Function not permitted in attach-message mode."); \
74 struct q_class_t *next, *prev;
75 struct q_class_t *down, *up;
90 struct syntax_t *syntax;
91 struct syntax_t *search;
92 struct q_class_t *quote;
95 #define ANSI_OFF (1<<0)
96 #define ANSI_BLINK (1<<1)
97 #define ANSI_BOLD (1<<2)
98 #define ANSI_UNDERLINE (1<<3)
99 #define ANSI_REVERSE (1<<4)
100 #define ANSI_COLOR (1<<5)
102 typedef struct _ansi_attr {
109 static short InHelp = 0;
111 static struct resize {
117 #define NumSigLines 4
119 static int check_sig(const char *s, struct line_t *info, int n)
123 while (n > 0 && count <= NumSigLines) {
124 if (info[n].type != MT_COLOR_SIGNATURE)
133 if (count > NumSigLines) {
134 /* check for a blank line */
143 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
144 int special, ansi_attr * a)
146 int def_color; /* color without syntax hilight */
147 int color; /* final color */
148 static int last_color; /* last color set */
149 int search = 0, i, m;
152 last_color = -1; /* force attrset() */
154 if (lineInfo[n].continuation) {
155 if (!cnt && option (OPTMARKERS)) {
156 SETCOLOR (MT_COLOR_MARKERS);
158 last_color = ColorDefs[MT_COLOR_MARKERS];
160 m = (lineInfo[n].syntax)[0].first;
161 cnt += (lineInfo[n].syntax)[0].last;
166 if (!(flags & M_SHOWCOLOR))
167 def_color = ColorDefs[MT_COLOR_NORMAL];
168 else if (lineInfo[m].type == MT_COLOR_HEADER)
169 def_color = (lineInfo[m].syntax)[0].color;
171 def_color = ColorDefs[lineInfo[m].type];
173 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
174 struct q_class_t *class = lineInfo[m].quote;
177 def_color = class->color;
179 while (class && class->length > cnt) {
180 def_color = class->color;
187 if (flags & M_SHOWCOLOR) {
188 for (i = 0; i < lineInfo[m].chunks; i++) {
189 /* we assume the chunks are sorted */
190 if (cnt > (lineInfo[m].syntax)[i].last)
192 if (cnt < (lineInfo[m].syntax)[i].first)
194 if (cnt != (lineInfo[m].syntax)[i].last) {
195 color = (lineInfo[m].syntax)[i].color;
198 /* don't break here, as cnt might be in the next chunk as well */
202 if (flags & M_SEARCH) {
203 for (i = 0; i < lineInfo[m].search_cnt; i++) {
204 if (cnt > (lineInfo[m].search)[i].last)
206 if (cnt < (lineInfo[m].search)[i].first)
208 if (cnt != (lineInfo[m].search)[i].last) {
209 color = ColorDefs[MT_COLOR_SEARCH];
216 /* handle "special" bold & underlined characters */
217 if (special || a->attr) {
218 if ((a->attr & ANSI_COLOR)) {
220 a->pair = mutt_alloc_color (a->fg, a->bg);
222 if (a->attr & ANSI_BOLD)
225 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
226 if (ColorDefs[MT_COLOR_BOLD] && !search)
227 color = ColorDefs[MT_COLOR_BOLD];
231 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
232 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
233 color = ColorDefs[MT_COLOR_UNDERLINE];
235 color ^= A_UNDERLINE;
237 else if (a->attr & ANSI_REVERSE) {
240 else if (a->attr & ANSI_BLINK) {
243 else if (a->attr & ANSI_OFF) {
248 if (color != last_color) {
254 static void append_line (struct line_t *lineInfo, int n, int cnt)
258 lineInfo[n + 1].type = lineInfo[n].type;
259 lineInfo[n + 1].syntax[0].color = lineInfo[n].syntax[0].color;
260 lineInfo[n + 1].continuation = 1;
262 /* find the real start of the line */
263 for (m = n; m >= 0; m--)
264 if (lineInfo[m].continuation == 0)
267 (lineInfo[n + 1].syntax)[0].first = m;
268 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
269 cnt + (lineInfo[n].syntax)[0].last : cnt;
272 static void new_class_color (struct q_class_t *class, int *q_level)
274 class->index = (*q_level)++;
275 class->color = ColorQuote[class->index % ColorQuoteUsed];
279 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
280 int lindex, int *q_level)
282 struct q_class_t *q_list;
285 new_class->index = -1;
288 if (q_list->index >= lindex) {
290 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
293 q_list = q_list->down;
294 else if (q_list->next)
295 q_list = q_list->next;
297 while (!q_list->next) {
303 q_list = q_list->next;
307 new_class->index = lindex;
308 new_class->color = ColorQuote[lindex % ColorQuoteUsed];
312 static void cleanup_quote(struct q_class_t **QuoteList)
315 struct q_class_t *ptr = (*QuoteList)->next;
316 cleanup_quote(&(*QuoteList)->down);
317 p_delete(&(*QuoteList)->prefix);
323 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
324 const char *qptr, int length,
325 int *force_redraw, int *q_level)
327 struct q_class_t *q_list = *QuoteList;
328 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
330 int offset, tail_lng;
333 if (ColorQuoteUsed <= 1) {
334 /* not much point in classifying quotes... */
336 if (*QuoteList == NULL) {
337 class = p_new(struct q_class_t, 1);
338 class->color = ColorQuote[0];
344 /* Did I mention how much I like emulating Lisp in C? */
346 /* classify quoting prefix */
348 if (length <= q_list->length) {
349 /* case 1: check the top level nodes */
351 if (m_strncmp(qptr, q_list->prefix, length) == 0) {
352 if (length == q_list->length)
353 return q_list; /* same prefix: return the current class */
355 /* found shorter prefix */
357 /* add a node above q_list */
358 tmp = p_new(struct q_class_t, 1);
359 tmp->prefix = p_dupstr(qptr, length);
360 tmp->length = length;
362 /* replace q_list by tmp in the top level list */
364 tmp->next = q_list->next;
365 q_list->next->prev = tmp;
368 tmp->prev = q_list->prev;
369 q_list->prev->next = tmp;
372 /* make q_list a child of tmp */
376 /* q_list has no siblings for now */
380 /* update the root if necessary */
381 if (q_list == *QuoteList)
384 lindex = q_list->index;
386 /* tmp should be the return class too */
389 /* next class to test; if tmp is a shorter prefix for another
390 * node, that node can only be in the top level list, so don't
391 * go down after this point
395 /* found another branch for which tmp is a shorter prefix */
397 /* save the next sibling for later */
400 /* unlink q_list from the top level list */
402 q_list->next->prev = q_list->prev;
404 q_list->prev->next = q_list->next;
406 /* at this point, we have a tmp->down; link q_list to it */
408 /* sibling order is important here, q_list should be linked last */
416 lindex = q_list->index;
418 /* next class to test; as above, we shouldn't go down */
422 /* we found a shorter prefix, so certain quotes have changed classes */
426 /* shorter, but not a substring of the current class: try next */
427 q_list = q_list->next;
431 /* case 2: try subclassing the current top level node */
433 /* tmp != NULL means we already found a shorter prefix at case 1 */
435 && m_strncmp(qptr, q_list->prefix, q_list->length) == 0) {
436 /* ok, it's a subclass somewhere on this branch */
439 offset = q_list->length;
441 q_list = q_list->down;
442 tail_lng = length - offset;
443 tail_qptr = (char *) qptr + offset;
446 if (length <= q_list->length) {
447 if (m_strncmp(tail_qptr, (q_list->prefix) + offset, tail_lng)
449 /* same prefix: return the current class */
450 if (length == q_list->length)
453 /* found shorter common prefix */
455 /* add a node above q_list */
456 tmp = p_new(struct q_class_t, 1);
457 tmp->prefix = p_dupstr(qptr, length);
458 tmp->length = length;
460 /* replace q_list by tmp */
462 tmp->next = q_list->next;
463 q_list->next->prev = tmp;
466 tmp->prev = q_list->prev;
467 q_list->prev->next = tmp;
470 /* make q_list a child of tmp */
472 tmp->up = q_list->up;
474 if (tmp->up->down == q_list)
477 /* q_list has no siblings */
481 lindex = q_list->index;
483 /* tmp should be the return class too */
486 /* next class to test */
489 /* found another branch for which tmp is a shorter prefix */
491 /* save the next sibling for later */
494 /* unlink q_list from the top level list */
496 q_list->next->prev = q_list->prev;
498 q_list->prev->next = q_list->next;
500 /* at this point, we have a tmp->down; link q_list to it */
509 lindex = q_list->index;
511 /* next class to test */
515 /* we found a shorter prefix, so we need a redraw */
519 q_list = q_list->next;
523 /* longer than the current prefix: try subclassing it */
525 && m_strncmp(tail_qptr, (q_list->prefix) + offset,
526 q_list->length - offset) == 0) {
527 /* still a subclass: go down one level */
529 offset = q_list->length;
531 q_list = q_list->down;
532 tail_lng = length - offset;
533 tail_qptr = (char *) qptr + offset;
537 /* nope, try the next prefix */
538 q_list = q_list->next;
544 /* still not found so far: add it as a sibling to the current node */
546 tmp = p_new(struct q_class_t, 1);
547 tmp->prefix = p_dupstr(qptr, length);
548 tmp->length = length;
551 tmp->next = ptr->down;
552 ptr->down->prev = tmp;
557 new_class_color (tmp, q_level);
562 shift_class_colors (*QuoteList, tmp, lindex, q_level);
567 /* nope, try the next prefix */
568 q_list = q_list->next;
575 /* not found so far: add it as a top level class */
576 class = p_new(struct q_class_t, 1);
577 class->prefix = p_dupstr(qptr, length);
578 class->length = length;
579 new_class_color (class, q_level);
582 class->next = *QuoteList;
583 (*QuoteList)->prev = class;
589 shift_class_colors (*QuoteList, tmp, lindex, q_level);
594 static int brailleLine = -1;
595 static int brailleCol = -1;
597 static int check_attachment_marker (char *);
600 resolve_types (char *buf, char *rawbuf, struct line_t *lineInfo, int n, int last,
601 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
604 COLOR_LINE *color_line;
605 regmatch_t pmatch[1], smatch[1];
606 int found, offset, null_rx, i;
608 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
609 if (buf[0] == '\n') {
610 lineInfo[n].type = MT_COLOR_NORMAL;
611 getyx(stdscr, brailleLine, brailleCol);
613 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
614 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
615 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
617 lineInfo[n].type = MT_COLOR_HDEFAULT;
618 color_line = ColorHdrList;
620 if (REGEXEC (&color_line->rx, buf) == 0) {
621 lineInfo[n].type = MT_COLOR_HEADER;
622 lineInfo[n].syntax[0].color = color_line->pair;
625 color_line = color_line->next;
629 else if (m_strncmp("\033[0m", rawbuf, 4) == 0) /* a little hack... */
630 lineInfo[n].type = MT_COLOR_NORMAL;
631 else if (check_attachment_marker ((char *) rawbuf) == 0)
632 lineInfo[n].type = MT_COLOR_ATTACHMENT;
633 else if (m_strcmp("-- \n", buf) == 0
634 || m_strcmp("-- \r\n", buf) == 0) {
637 lineInfo[n].type = MT_COLOR_SIGNATURE;
638 while (i < last && check_sig(buf, lineInfo, i - 1) == 0 &&
639 (lineInfo[i].type == MT_COLOR_NORMAL ||
640 lineInfo[i].type == MT_COLOR_QUOTED ||
641 lineInfo[i].type == MT_COLOR_HEADER)) {
643 if (lineInfo[i].chunks) {
644 lineInfo[i].chunks = 0;
645 p_realloc(&(lineInfo[n].syntax), 1);
647 lineInfo[i++].type = MT_COLOR_SIGNATURE;
650 else if (check_sig(buf, lineInfo, n - 1) == 0)
651 lineInfo[n].type = MT_COLOR_SIGNATURE;
652 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
653 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
654 if (smatch[0].rm_so > 0) {
657 /* hack to avoid making an extra copy of buf */
658 c = buf[smatch[0].rm_so];
659 buf[smatch[0].rm_so] = 0;
661 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
662 if (q_classify && lineInfo[n].quote == NULL)
663 lineInfo[n].quote = classify_quote (QuoteList,
664 buf + pmatch[0].rm_so,
666 pmatch[0].rm_so, force_redraw,
668 lineInfo[n].type = MT_COLOR_QUOTED;
670 lineInfo[n].type = MT_COLOR_NORMAL;
673 buf[smatch[0].rm_so] = c;
675 lineInfo[n].type = MT_COLOR_NORMAL;
678 if (q_classify && lineInfo[n].quote == NULL)
679 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
680 pmatch[0].rm_eo - pmatch[0].rm_so,
681 force_redraw, q_level);
682 lineInfo[n].type = MT_COLOR_QUOTED;
685 lineInfo[n].type = MT_COLOR_NORMAL;
689 if (lineInfo[n].type == MT_COLOR_NORMAL ||
690 lineInfo[n].type == MT_COLOR_QUOTED) {
694 lineInfo[n].chunks = 0;
701 color_line = ColorBodyList;
703 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
704 (offset ? REG_NOTBOL : 0)) == 0) {
705 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
707 if (++(lineInfo[n].chunks) > 1)
708 p_realloc(&(lineInfo[n].syntax), lineInfo[n].chunks);
710 i = lineInfo[n].chunks - 1;
711 pmatch[0].rm_so += offset;
712 pmatch[0].rm_eo += offset;
714 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
715 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
716 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
717 (lineInfo[n].syntax)[i].color = color_line->pair;
718 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
719 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
724 null_rx = 1; /* empty regexp; don't add it, but keep looking */
727 color_line = color_line->next;
731 offset++; /* avoid degenerate cases */
733 offset = lineInfo[n].syntax[i].last;
734 } while (found || null_rx);
738 static int is_ansi(unsigned char *buf)
740 while (isdigit(*buf) || *buf == ';')
742 return (*buf == 'm');
745 static int check_attachment_marker(char *p)
747 char *q = AttachmentMarker;
749 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
750 return (int)(*p - *q);
753 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
757 while (isdigit (buf[x]) || buf[x] == ';')
760 /* Character Attributes */
761 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
764 mutt_free_color (a->fg, a->bg);
771 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
772 a->attr |= ANSI_BOLD;
775 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
776 a->attr |= ANSI_UNDERLINE;
779 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
780 a->attr |= ANSI_BLINK;
783 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
784 a->attr |= ANSI_REVERSE;
787 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
792 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
794 mutt_free_color (a->fg, a->bg);
796 a->attr |= ANSI_COLOR;
797 if (buf[pos + 1] != '9')
798 a->fg = buf[pos + 1] - '0';
801 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
803 mutt_free_color (a->fg, a->bg);
805 a->attr |= ANSI_COLOR;
806 if (buf[pos + 1] != '9')
807 a->bg = buf[pos + 1] - '0';
810 while (pos < x && buf[pos] != ';')
820 /* trim tail of buf so that it contains complete multibyte characters */
821 static int trim_incomplete_mbyte(unsigned char *buf, size_t len) {
825 p_clear(&mbstate, 1);
826 for (; len > 0; buf += k, len -= k) {
827 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
830 if (k == -1 || k == 0)
839 fill_buffer (FILE * f, off_t *last_pos, off_t offset, unsigned char *buf,
840 unsigned char *fmt, ssize_t blen, int *buf_ready)
843 static int b_read = 0;
845 if (*buf_ready == 0) {
847 if (offset != *last_pos)
848 fseeko (f, offset, 0);
849 if (fgets ((char *) buf, blen - 1, f) == NULL) {
853 *last_pos = ftello (f);
854 b_read = (int) (*last_pos - offset);
857 /* incomplete mbyte characters trigger a segfault in regex processing for
858 * certain versions of glibc. Trim them if necessary. */
859 if (b_read == blen - 2)
860 b_read -= trim_incomplete_mbyte(buf, b_read);
862 /* copy "buf" to "fmt", but without bold and underline controls */
865 if (*p == '\010' && (p > buf)) {
866 if (*(p + 1) == '_') /* underline */
868 else if (*(p + 1)) { /* bold or overstrike */
869 *(fmt - 1) = *(p + 1);
875 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
876 while (*p++ != 'm') /* skip ANSI sequence */
879 else if (*p == '\033' && *(p + 1) == ']'
880 && check_attachment_marker ((char *) p) == 0) {
881 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
896 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
897 int flags, ansi_attr * pa, int cnt,
898 int *pspace, int *pvch, int *pcol, int *pspecial)
900 int space = -1; /* index of the last space or TAB */
901 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
902 int ch, vch, k, last_special = -1, special = 0, t;
906 int wrap_cols = COLS;
908 if (!(flags & (M_SHOWFLAT)))
909 wrap_cols -= WrapMargin;
915 /* FIXME: this should come from lineInfo */
916 p_clear(&mbstate, 1);
918 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
919 /* Handle ANSI sequences */
920 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
921 is_ansi (buf + ch + 2))
922 ch = grok_ansi (buf, ch + 2, pa) + 1;
924 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
925 check_attachment_marker ((char *) buf + ch) == 0) {
926 while (buf[ch++] != '\a')
931 /* is anything left to do? */
935 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
936 if (k == -2 || k == -1) {
937 if (col + 4 > wrap_cols)
941 printw ("\\%03o", buf[ch]);
948 /* Handle backspace */
955 while ((wc1 = 0, mbstate1 = mbstate,
957 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
958 &mbstate1), k1 - k > 0 && wc1 == '\b')
960 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
961 &mbstate1), k2 > 0 && iswprint(wc1))) {
963 special |= (wc == '_' && special & A_UNDERLINE)
964 ? A_UNDERLINE : A_BOLD;
966 else if (wc == '_' || wc1 == '_') {
967 special |= A_UNDERLINE;
968 wc = (wc1 == '_') ? wc : wc1;
970 /* special = 0; / * overstrike: nothing to do! */
980 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
981 special || last_special || pa->attr)) {
982 resolve_color (*lineInfo, n, vch, flags, special, pa);
983 last_special = special;
990 if (col + t > wrap_cols)
998 else if (wc == '\t') {
1004 for (; col < t; col++)
1009 else if (wc < 0x20 || wc == 0x7f) {
1010 if (col + 2 > wrap_cols)
1014 printw ("^%c", ('@' + wc) & 0x7f);
1016 else if (wc < 0x100) {
1017 if (col + 4 > wrap_cols)
1021 printw ("\\%03o", wc);
1023 if (col + 1 > wrap_cols)
1027 addch (CharsetReplacement);
1033 *pspecial = special;
1039 * flags M_SHOWFLAT, show characters (used for displaying help)
1040 * M_SHOWCOLOR, show characters in color
1041 * otherwise don't show characters
1042 * M_HIDE, don't show quoted text
1043 * M_SEARCH, resolve search patterns
1044 * M_TYPES, compute line's type
1045 * M_PAGER_NSKIP, keeps leading whitespace
1046 * M_PAGER_MARKER, eventually show markers
1049 * -1 EOF was reached
1050 * 0 normal exit, line was not displayed
1051 * >0 normal exit, line was displayed
1054 display_line (FILE * f, off_t *last_pos, struct line_t **lineInfo, int n,
1055 int *last, int *max, int flags, struct q_class_t **QuoteList,
1056 int *q_level, int *force_redraw, regex_t * SearchRE)
1058 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1059 unsigned char *buf_ptr = buf;
1060 int ch, vch, col, cnt, b_read;
1061 int buf_ready = 0, change_last = 0;
1066 ansi_attr a = { 0, 0, 0, -1 };
1067 regmatch_t pmatch[1];
1074 if (*last == *max) {
1075 p_realloc(lineInfo, *max += LINES);
1076 for (ch = *last; ch < *max; ch++) {
1077 p_clear(&(*lineInfo)[ch], 1);
1078 (*lineInfo)[ch].type = -1;
1079 (*lineInfo)[ch].search_cnt = -1;
1080 (*lineInfo)[ch].syntax = p_new(struct syntax_t, 1);
1081 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1086 /* only do color hiliting if we are viewing a message */
1087 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1088 if ((*lineInfo)[n].type == -1) {
1089 /* determine the line class */
1091 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1098 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1099 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1101 /* avoid race condition for continuation lines when scrolling up */
1103 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1105 (*lineInfo)[m].type = (*lineInfo)[n].type;
1108 /* this also prevents searching through the hidden lines */
1109 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1110 flags = 0; /* M_NOSHOW */
1113 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1114 * don't want to compute it every time M_TYPES is set, since this
1115 * would slow down the "bottom" function unacceptably. A compromise
1116 * solution is hence to call regexec() again, just to find out the
1117 * length of the quote prefix.
1119 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1120 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1123 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1129 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1130 (*lineInfo)[n].quote = classify_quote (QuoteList,
1131 (char *) fmt + pmatch[0].rm_so,
1132 pmatch[0].rm_eo - pmatch[0].rm_so,
1133 force_redraw, q_level);
1136 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1137 && (*lineInfo)[n].search_cnt == -1) {
1139 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1147 (*lineInfo)[n].search_cnt = 0;
1149 (SearchRE, (char *) fmt + offset, 1, pmatch,
1150 (offset ? REG_NOTBOL : 0)) == 0) {
1151 if (++((*lineInfo)[n].search_cnt) > 1)
1152 p_realloc(&(*lineInfo)[n].search, (*lineInfo)[n].search_cnt);
1154 (*lineInfo)[n].search = p_new(struct syntax_t, 1);
1155 pmatch[0].rm_so += offset;
1156 pmatch[0].rm_eo += offset;
1157 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1159 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1162 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1163 offset++; /* avoid degenerate cases */
1165 offset = pmatch[0].rm_eo;
1171 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1172 /* we've already scanned this line, so just exit */
1175 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1176 /* no need to try to display this line... */
1177 return (1); /* fake display */
1180 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1181 sizeof (buf), &buf_ready)) < 0) {
1187 /* now chose a good place to break the line */
1188 cnt = format_line(lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1190 buf_ptr = buf + cnt;
1192 /* move the break point only if smart_wrap is set */
1193 if (option (OPTWRAP)) {
1195 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1196 && buf[cnt] != '\r') {
1198 /* skip trailing blanks */
1199 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1201 /* a very long word with leading spaces causes infinite wrapping */
1202 if ((!ch) && (flags & M_PAGER_NSKIP))
1203 buf_ptr = buf + cnt;
1207 buf_ptr = buf + cnt; /* a very long word... */
1210 if (!(flags & M_PAGER_NSKIP))
1211 /* skip leading blanks on the next line too */
1212 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1216 if (*buf_ptr == '\r')
1218 if (*buf_ptr == '\n')
1221 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1222 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1223 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1225 /* if we don't need to display the line we are done */
1226 if (!(flags & M_SHOW))
1229 /* display the line */
1230 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1232 /* end the last color pattern (needed by S-Lang) */
1233 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1234 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1237 * Fill the blank space at the end of the line with the prevailing color.
1238 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1239 * to make sure to reset the color *after* that
1241 if (flags & M_SHOWCOLOR) {
1242 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1243 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1244 def_color = ((*lineInfo)[m].syntax)[0].color;
1246 def_color = ColorDefs[(*lineInfo)[m].type];
1248 attrset (def_color);
1250 bkgdset (def_color | ' ');
1256 * reset the color back to normal. This *must* come after the
1257 * addch('\n'), otherwise the color for this line will not be
1258 * filled to the right margin.
1260 if (flags & M_SHOWCOLOR) {
1261 SETCOLOR (MT_COLOR_NORMAL);
1262 BKGDSET (MT_COLOR_NORMAL);
1265 /* build a return code */
1266 if (!(flags & M_SHOW))
1272 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1274 while (cur > 0 && nlines > 0) {
1276 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1283 static struct mapping_t PagerHelp[] = {
1284 {N_("Exit"), OP_EXIT},
1285 {N_("PrevPg"), OP_PREV_PAGE},
1286 {N_("NextPg"), OP_NEXT_PAGE},
1289 static struct mapping_t PagerHelpExtra[] = {
1290 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1291 {N_("Del"), OP_DELETE},
1292 {N_("Reply"), OP_REPLY},
1293 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1298 static struct mapping_t PagerNewsHelpExtra[] = {
1299 {N_("Post"), OP_POST},
1300 {N_("Followup"), OP_FOLLOWUP},
1301 {N_("Del"), OP_DELETE},
1302 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1308 /* This pager is actually not so simple as it once was. It now operates in
1309 two modes: one for viewing messages and the other for viewing help. These
1310 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1311 is there so that we can do operations on the current message without the
1312 need to pop back out to the main-menu. */
1314 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1316 static char searchbuf[STRING];
1317 char buffer[LONG_STRING];
1318 char helpstr[STRING * 2];
1319 char tmphelp[STRING * 2];
1320 int maxLine, lastLine = 0;
1321 struct line_t *lineInfo;
1322 struct q_class_t *QuoteList = NULL;
1323 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1324 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1326 int redraw = REDRAW_FULL;
1328 off_t last_pos = 0, last_offset = 0;
1329 int old_smart_wrap, old_markers;
1332 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1333 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1335 int bodyoffset = 1; /* offset of first line of real text */
1336 int statusoffset = 0; /* offset for the status bar */
1337 int helpoffset = LINES - 2; /* offset for the help bar. */
1338 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1340 MUTTMENU *pager_index = NULL; /* the Pager Index (PI) */
1341 int indexoffset = 0; /* offset for the PI */
1342 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1343 int indicator = indexlen / 3; /* the indicator line of the PI */
1344 int old_PagerIndexLines; /* some people want to resize it
1345 * while inside the pager... */
1351 if (!(flags & M_SHOWCOLOR))
1352 flags |= M_SHOWFLAT;
1354 if ((fp = fopen (fname, "r")) == NULL) {
1355 mutt_perror (fname);
1359 if (stat (fname, &sb) != 0) {
1360 mutt_perror (fname);
1366 /* Initialize variables */
1368 if (IsHeader (extra) && !extra->hdr->read) {
1369 Context->msgnotreadyet = extra->hdr->msgno;
1370 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1373 lineInfo = p_new(struct line_t, maxLine = LINES);
1374 for (i = 0; i < maxLine; i++) {
1375 p_clear(&lineInfo[i], 1);
1376 lineInfo[i].type = -1;
1377 lineInfo[i].search_cnt = -1;
1378 lineInfo[i].syntax = p_new(struct syntax_t, 1);
1379 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1382 mutt_compile_help (helpstr, sizeof(helpstr), MENU_PAGER, PagerHelp);
1383 if (IsHeader (extra)) {
1384 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1385 mutt_compile_help (buffer, sizeof(buffer), MENU_PAGER,
1388 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1391 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1394 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1395 mutt_make_help (buffer, sizeof(buffer), _("Help"), MENU_PAGER, OP_HELP);
1396 snprintf (helpstr, sizeof(helpstr), "%s %s", tmphelp, buffer);
1403 if (redraw & REDRAW_FULL) {
1404 SETCOLOR (MT_COLOR_NORMAL);
1405 /* clear() doesn't optimize screen redraws */
1409 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1410 indexlen = Context->vcount + 1;
1412 indexlen = PagerIndexLines;
1414 indicator = indexlen / 3;
1416 if (option (OPTSTATUSONTOP)) {
1418 statusoffset = IsHeader (extra) ? indexlen : 0;
1419 bodyoffset = statusoffset + 1;
1420 helpoffset = LINES - 2;
1421 bodylen = helpoffset - bodyoffset;
1422 if (!option (OPTHELP))
1427 statusoffset = LINES - 2;
1428 if (!option (OPTHELP))
1430 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1431 bodylen = statusoffset - bodyoffset;
1434 if (option (OPTHELP)) {
1435 SETCOLOR (MT_COLOR_STATUS);
1436 move (helpoffset, SW);
1437 mutt_paddstr (COLS-SW, helpstr);
1438 SETCOLOR (MT_COLOR_NORMAL);
1441 if (Resize != NULL) {
1442 if ((SearchCompiled = Resize->SearchCompiled)) {
1444 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1445 SearchFlag = M_SEARCH;
1446 SearchBack = Resize->SearchBack;
1448 lines = Resize->line;
1449 redraw |= REDRAW_SIGWINCH;
1454 if (IsHeader (extra) && PagerIndexLines) {
1455 if (pager_index == NULL) {
1456 /* only allocate the space if/when we need the index.
1457 Initialise the menu as per the main index */
1458 pager_index = mutt_new_menu ();
1459 pager_index->menu = MENU_MAIN;
1460 pager_index->make_entry = index_make_entry;
1461 pager_index->color = index_color;
1462 pager_index->max = Context->vcount;
1463 pager_index->current = extra->hdr->virtual;
1466 SETCOLOR (MT_COLOR_NORMAL);
1467 pager_index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1469 pager_index->pagelen = indexlen - 1;
1471 /* some fudge to work out where abouts the indicator should go */
1472 if (pager_index->current - indicator < 0)
1473 pager_index->top = 0;
1474 else if (pager_index->max - pager_index->current < pager_index->pagelen - indicator)
1475 pager_index->top = pager_index->max - pager_index->pagelen;
1477 pager_index->top = pager_index->current - indicator;
1479 menu_redraw_index (pager_index);
1482 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1486 if (redraw & REDRAW_SIGWINCH) {
1489 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1490 has_types | SearchFlag, &QuoteList, &q_level,
1491 &force_redraw, &SearchRE) == 0) {
1492 if (!lineInfo[i].continuation && ++j == lines) {
1497 redraw |= REDRAW_SIDEBAR;
1501 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1503 move (bodyoffset, SW);
1504 curline = oldtopline = topline;
1508 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1509 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1511 (flags & M_DISPLAYFLAGS) | hideQuoted |
1512 SearchFlag, &QuoteList, &q_level, &force_redraw,
1516 move (lines + bodyoffset, SW);
1517 redraw |= REDRAW_SIDEBAR;
1519 last_offset = lineInfo[curline].offset;
1520 } while (force_redraw);
1522 SETCOLOR (MT_COLOR_TILDE);
1523 BKGDSET (MT_COLOR_TILDE);
1524 while (lines < bodylen) {
1526 if (option (OPTTILDE))
1530 move (lines + bodyoffset, SW);
1532 /* We are going to update the pager status bar, so it isn't
1533 * necessary to reset to normal color now. */
1535 redraw |= REDRAW_STATUS; /* need to update the % seen */
1538 if (redraw & REDRAW_STATUS) {
1539 /* print out the pager status bar */
1540 SETCOLOR (MT_COLOR_STATUS);
1541 BKGDSET (MT_COLOR_STATUS);
1542 CLEARLINE_WIN (statusoffset);
1543 if (IsHeader (extra)) {
1544 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1545 size_t l2 = sizeof (buffer);
1547 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1548 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1550 else if (IsMsgAttach (extra)) {
1551 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1552 size_t l2 = sizeof (buffer);
1554 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1555 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1557 move(statusoffset,SW);
1558 mutt_paddstr (COLS - 10 - SW, IsHeader (extra) ||
1559 IsMsgAttach (extra) ? buffer : banner);
1562 if (last_pos < sb.st_size - 1)
1563 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1565 addstr (topline == 0 ? "all)" : "end)");
1566 BKGDSET (MT_COLOR_NORMAL);
1567 SETCOLOR (MT_COLOR_NORMAL);
1570 if (redraw & REDRAW_SIDEBAR)
1573 if ((redraw & REDRAW_INDEX) && pager_index) {
1574 /* redraw the pager_index indicator, because the
1575 * flags for this message might have changed. */
1576 menu_redraw_current (pager_index);
1578 /* print out the pager_index status bar */
1579 menu_status_line (buffer, sizeof (buffer), pager_index, NONULL (Status));
1580 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), SW);
1581 SETCOLOR (MT_COLOR_STATUS);
1582 BKGDSET (MT_COLOR_STATUS);
1583 mutt_paddstr (COLS-SW, buffer);
1584 SETCOLOR (MT_COLOR_NORMAL);
1585 BKGDSET (MT_COLOR_NORMAL);
1587 /* if we're not using the index, update every time */
1593 if (option(OPTBRAILLEFRIENDLY)) {
1594 if (brailleLine!=-1) {
1595 move(brailleLine+1, 0);
1599 move (statusoffset, COLS-1);
1602 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1603 && lineInfo[curline].offset < sb.st_size-1) {
1604 if (TopLine - topline > lines)
1613 ch = km_dokey (MENU_PAGER);
1615 mutt_clear_error ();
1622 else if (SigWinch) {
1623 mutt_resize_screen ();
1625 /* Store current position. */
1627 for (i = 0; i <= topline; i++)
1628 if (!lineInfo[i].continuation)
1631 if (flags & M_PAGER_RETWINCH) {
1632 Resize = p_new(struct resize, 1);
1634 Resize->line = lines;
1635 Resize->SearchCompiled = SearchCompiled;
1636 Resize->SearchBack = SearchBack;
1639 rc = OP_REFORMAT_WINCH;
1641 for (i = 0; i < maxLine; i++) {
1642 lineInfo[i].offset = 0;
1643 lineInfo[i].type = -1;
1644 lineInfo[i].continuation = 0;
1645 lineInfo[i].chunks = 0;
1646 lineInfo[i].search_cnt = -1;
1647 lineInfo[i].quote = NULL;
1649 p_realloc(&lineInfo[i].syntax, 1);
1651 p_delete(&lineInfo[i].search);
1657 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1662 clearok (stdscr, TRUE); /*force complete redraw */
1665 else if (ch == -1) {
1679 if (lineInfo[curline].offset < sb.st_size - 1) {
1680 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1682 else if (option (OPTPAGERSTOP)) {
1683 /* emulate "less -q" and don't go on to the next message. */
1684 mutt_error _("Bottom of message is shown.");
1686 /* end of the current message, so display the next message. */
1687 rc = OP_MAIN_NEXT_UNDELETED;
1695 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1698 mutt_error _("Top of message is shown.");
1702 if (lineInfo[curline].offset < sb.st_size - 1) {
1705 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1711 mutt_error _("Bottom of message is shown.");
1716 topline = upNLines (1, lineInfo, topline, hideQuoted);
1718 mutt_error _("Top of message is shown.");
1725 mutt_error _("Top of message is shown.");
1730 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1732 mutt_error _("Top of message is shown.");
1736 if (lineInfo[curline].offset < sb.st_size - 1) {
1737 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1739 else if (option (OPTPAGERSTOP)) {
1740 /* emulate "less -q" and don't go on to the next message. */
1741 mutt_error _("Bottom of message is shown.");
1743 /* end of the current message, so display the next message. */
1744 rc = OP_MAIN_NEXT_UNDELETED;
1749 case OP_SEARCH_NEXT:
1750 case OP_SEARCH_OPPOSITE:
1751 if (SearchCompiled) {
1753 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1754 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1755 /* searching forward */
1756 for (i = topline + 1; i < lastLine; i++) {
1757 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1758 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1765 mutt_error _("Not found.");
1768 /* searching backward */
1769 for (i = topline - 1; i >= 0; i--) {
1770 if ((!hideQuoted || (has_types &&
1771 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1772 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1779 mutt_error _("Not found.");
1782 if (lineInfo[topline].search_cnt > 0)
1783 SearchFlag = M_SEARCH;
1787 /* no previous search pattern, so fall through to search */
1790 case OP_SEARCH_REVERSE:
1791 m_strcpy(buffer, sizeof(buffer), searchbuf);
1792 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1793 _("Search: ")), buffer, sizeof (buffer),
1797 if (!m_strcmp (buffer, searchbuf)) {
1798 if (SearchCompiled) {
1799 /* do an implicit search-next */
1800 if (ch == OP_SEARCH)
1801 ch = OP_SEARCH_NEXT;
1803 ch = OP_SEARCH_OPPOSITE;
1812 m_strcpy(searchbuf, sizeof(searchbuf), buffer);
1814 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1815 if (ch == OP_SEARCH)
1817 else if (ch == OP_SEARCH_REVERSE)
1820 if (SearchCompiled) {
1821 regfree (&SearchRE);
1822 for (i = 0; i < lastLine; i++) {
1823 p_delete(&lineInfo[i].search);
1824 lineInfo[i].search_cnt = -1;
1829 REGCOMP (&SearchRE, searchbuf,
1830 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1831 regerror (err, &SearchRE, buffer, sizeof (buffer));
1832 mutt_error ("%s", buffer);
1833 regfree (&SearchRE);
1834 for (i = 0; i < maxLine; i++) {
1836 p_delete(&lineInfo[i].search);
1837 lineInfo[i].search_cnt = -1;
1844 /* update the search pointers */
1846 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1847 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1848 &QuoteList, &q_level,
1849 &force_redraw, &SearchRE) == 0) {
1851 redraw |= REDRAW_SIDEBAR;
1855 /* searching forward */
1856 for (i = topline; i < lastLine; i++) {
1857 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1858 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1866 /* searching backward */
1867 for (i = topline; i >= 0; i--) {
1868 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1869 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1877 if (lineInfo[topline].search_cnt == 0) {
1879 mutt_error _("Not found.");
1882 SearchFlag = M_SEARCH;
1884 redraw = REDRAW_BODY;
1887 case OP_SEARCH_TOGGLE:
1888 if (SearchCompiled) {
1889 SearchFlag ^= M_SEARCH;
1890 redraw = REDRAW_BODY;
1895 /* don't let the user enter the help-menu from the help screen! */
1898 mutt_help (MENU_PAGER);
1899 redraw = REDRAW_FULL;
1903 mutt_error _("Help is currently being shown.");
1906 case OP_PAGER_HIDE_QUOTED:
1908 hideQuoted ^= M_HIDE;
1909 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1910 topline = upNLines (1, lineInfo, topline, hideQuoted);
1912 redraw = REDRAW_BODY;
1916 case OP_PAGER_SKIP_QUOTED:
1919 int new_topline = topline;
1921 while ((new_topline < lastLine ||
1922 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1923 new_topline, &lastLine,
1924 &maxLine, M_TYPES, &QuoteList,
1925 &q_level, &force_redraw,
1927 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
1928 redraw |= REDRAW_SIDEBAR;
1933 mutt_error _("No more quoted text.");
1938 while ((new_topline < lastLine ||
1939 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1940 new_topline, &lastLine,
1941 &maxLine, M_TYPES, &QuoteList,
1942 &q_level, &force_redraw,
1944 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
1946 redraw |= REDRAW_SIDEBAR;
1950 mutt_error _("No more unquoted text after quoted text.");
1954 topline = new_topline;
1958 case OP_PAGER_BOTTOM: /* move to the end of the file */
1959 if (lineInfo[curline].offset < sb.st_size - 1) {
1961 /* make sure the types are defined to the end of file */
1962 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1963 &maxLine, has_types,
1964 &QuoteList, &q_level, &force_redraw,
1967 redraw |= REDRAW_SIDEBAR;
1969 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
1972 mutt_error _("Bottom of message is shown.");
1976 clearok (stdscr, TRUE);
1977 redraw = REDRAW_FULL;
1981 km_error_key (MENU_PAGER);
1984 /* --------------------------------------------------------------------
1985 * The following are operations on the current message rather than
1986 * adjusting the view of the message.
1989 case OP_BOUNCE_MESSAGE:
1990 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
1992 if (IsMsgAttach (extra))
1993 mutt_attach_bounce (extra->fp, extra->hdr,
1994 extra->idx, extra->idxlen, extra->bdy);
1996 ci_bounce_message (extra->hdr, &redraw);
2000 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2002 if (IsMsgAttach (extra))
2003 mutt_attach_resend (extra->fp, extra->hdr,
2004 extra->idx, extra->idxlen, extra->bdy);
2006 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2007 redraw = REDRAW_FULL;
2010 case OP_CHECK_TRADITIONAL:
2011 CHECK_MODE (IsHeader (extra));
2012 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2014 rc = OP_CHECK_TRADITIONAL;
2018 case OP_CREATE_ALIAS:
2019 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2020 if (IsMsgAttach (extra))
2021 mutt_create_alias (extra->bdy->hdr->env, NULL);
2023 mutt_create_alias (extra->hdr->env, NULL);
2024 MAYBE_REDRAW (redraw);
2027 case OP_PURGE_MESSAGE:
2029 CHECK_MODE (IsHeader (extra));
2032 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2034 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2035 mutt_set_flag (Context, extra->hdr, M_PURGED,
2036 ch != OP_PURGE_MESSAGE ? 0 : 1);
2037 if (option (OPTDELETEUNTAG))
2038 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2039 redraw = REDRAW_STATUS | REDRAW_INDEX;
2040 if (option (OPTRESOLVE)) {
2042 rc = OP_MAIN_NEXT_UNDELETED;
2046 case OP_DELETE_THREAD:
2047 case OP_DELETE_SUBTHREAD:
2048 CHECK_MODE (IsHeader (extra));
2051 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2053 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2054 ch == OP_DELETE_THREAD ? 0 : 1);
2057 if (option (OPTDELETEUNTAG))
2058 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2059 ch == OP_DELETE_THREAD ? 0 : 1);
2060 if (option (OPTRESOLVE)) {
2061 rc = OP_MAIN_NEXT_UNDELETED;
2065 if (!option (OPTRESOLVE) && PagerIndexLines)
2066 redraw = REDRAW_FULL;
2068 redraw = REDRAW_STATUS | REDRAW_INDEX;
2072 case OP_DISPLAY_ADDRESS:
2073 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2074 if (IsMsgAttach (extra))
2075 mutt_display_address (extra->bdy->hdr->env);
2077 mutt_display_address (extra->hdr->env);
2080 case OP_ENTER_COMMAND:
2081 old_smart_wrap = option (OPTWRAP);
2082 old_markers = option (OPTMARKERS);
2083 old_PagerIndexLines = PagerIndexLines;
2085 CurrentMenu = MENU_PAGER;
2086 mutt_enter_command ();
2088 if (option (OPTNEEDRESORT)) {
2089 unset_option (OPTNEEDRESORT);
2090 CHECK_MODE (IsHeader (extra));
2091 set_option (OPTNEEDRESORT);
2094 if (old_PagerIndexLines != PagerIndexLines) {
2096 mutt_menuDestroy (&pager_index);
2100 if (option (OPTWRAP) != old_smart_wrap ||
2101 option (OPTMARKERS) != old_markers) {
2102 if (flags & M_PAGER_RETWINCH) {
2104 rc = OP_REFORMAT_WINCH;
2108 /* count the real lines above */
2110 for (i = 0; i <= topline; i++) {
2111 if (!lineInfo[i].continuation)
2115 /* we need to restart the whole thing */
2116 for (i = 0; i < maxLine; i++) {
2117 lineInfo[i].offset = 0;
2118 lineInfo[i].type = -1;
2119 lineInfo[i].continuation = 0;
2120 lineInfo[i].chunks = 0;
2121 lineInfo[i].search_cnt = -1;
2122 lineInfo[i].quote = NULL;
2124 p_realloc(&(lineInfo[i].syntax), 1);
2126 p_delete(&lineInfo[i].search);
2129 if (SearchCompiled) {
2130 regfree (&SearchRE);
2135 /* try to keep the old position */
2138 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2139 &lastLine, &maxLine,
2140 (has_types ? M_TYPES : 0),
2141 &QuoteList, &q_level, &force_redraw,
2143 redraw |= REDRAW_SIDEBAR;
2144 if (!lineInfo[topline].continuation)
2153 if (option (OPTFORCEREDRAWPAGER))
2154 redraw = REDRAW_FULL;
2155 unset_option (OPTFORCEREDRAWINDEX);
2156 unset_option (OPTFORCEREDRAWPAGER);
2159 case OP_FLAG_MESSAGE:
2160 CHECK_MODE (IsHeader (extra));
2163 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2165 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2166 redraw = REDRAW_STATUS | REDRAW_INDEX;
2167 if (option (OPTRESOLVE)) {
2169 rc = OP_MAIN_NEXT_UNDELETED;
2174 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2175 if (IsAttach (extra))
2176 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2178 mutt_pipe_message (extra->hdr);
2179 MAYBE_REDRAW (redraw);
2183 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2184 if (IsAttach (extra))
2185 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2187 mutt_print_message (extra->hdr);
2191 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2193 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2194 redraw = REDRAW_FULL;
2199 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2201 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2202 !((nntp_data_t *) extra->ctx->data)->allowed &&
2203 query_quadoption (OPT_TOMODERATED,
2205 ("Posting to this group not allowed, may be moderated. Continue?"))
2208 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2209 redraw = REDRAW_FULL;
2212 case OP_FORWARD_TO_GROUP:
2213 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2215 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2216 !((nntp_data_t *) extra->ctx->data)->allowed &&
2217 query_quadoption (OPT_TOMODERATED,
2219 ("Posting to this group not allowed, may be moderated. Continue?"))
2222 if (IsMsgAttach (extra))
2223 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2224 extra->idxlen, extra->bdy, SENDNEWS);
2226 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2228 redraw = REDRAW_FULL;
2232 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2235 if (IsMsgAttach (extra))
2236 followup_to = extra->bdy->hdr->env->followup_to;
2238 followup_to = extra->hdr->env->followup_to;
2240 if (!followup_to || m_strcasecmp(followup_to, "poster") ||
2241 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2242 _("Reply by mail as poster prefers?")) != M_YES) {
2243 if (extra->ctx && extra->ctx->magic == M_NNTP
2244 && !((nntp_data_t *) extra->ctx->data)->allowed
2245 && query_quadoption (OPT_TOMODERATED,
2247 ("Posting to this group not allowed, may be moderated. Continue?"))
2250 if (IsMsgAttach (extra))
2251 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2252 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2254 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2255 extra->ctx, extra->hdr);
2256 redraw = REDRAW_FULL;
2262 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2264 if (IsMsgAttach (extra))
2265 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2266 extra->idxlen, extra->bdy, SENDREPLY);
2268 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2269 redraw = REDRAW_FULL;
2272 case OP_RECALL_MESSAGE:
2273 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2275 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2276 redraw = REDRAW_FULL;
2279 case OP_GROUP_REPLY:
2280 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2282 if (IsMsgAttach (extra))
2283 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2284 extra->idxlen, extra->bdy,
2285 SENDREPLY | SENDGROUPREPLY);
2287 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2289 redraw = REDRAW_FULL;
2293 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2295 if (IsMsgAttach (extra))
2296 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2297 extra->idxlen, extra->bdy,
2298 SENDREPLY | SENDLISTREPLY);
2300 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2302 redraw = REDRAW_FULL;
2305 case OP_FORWARD_MESSAGE:
2306 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2308 if (IsMsgAttach (extra))
2309 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2310 extra->idxlen, extra->bdy, 0);
2312 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2313 redraw = REDRAW_FULL;
2316 case OP_DECRYPT_SAVE:
2318 if (IsAttach (extra)) {
2319 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2324 case OP_COPY_MESSAGE:
2325 case OP_DECODE_SAVE:
2326 case OP_DECODE_COPY:
2327 case OP_DECRYPT_COPY:
2328 CHECK_MODE (IsHeader (extra));
2329 if (mutt_save_message (extra->hdr,
2330 (ch == OP_DECRYPT_SAVE) ||
2331 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2332 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2333 (ch == OP_DECRYPT_SAVE)
2334 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2335 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2336 || ch == OP_DECRYPT_SAVE)) {
2337 if (option (OPTRESOLVE)) {
2339 rc = OP_MAIN_NEXT_UNDELETED;
2342 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2344 MAYBE_REDRAW (redraw);
2347 case OP_SHELL_ESCAPE:
2348 mutt_shell_escape ();
2349 MAYBE_REDRAW (redraw);
2353 CHECK_MODE (IsHeader (extra));
2354 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2356 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2357 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2358 ? NULL : Context->last_tag);
2360 redraw = REDRAW_STATUS | REDRAW_INDEX;
2361 if (option (OPTRESOLVE)) {
2368 CHECK_MODE (IsHeader (extra));
2371 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2373 if (extra->hdr->read || extra->hdr->old)
2374 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2376 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2378 Context->msgnotreadyet = -1;
2379 redraw = REDRAW_STATUS | REDRAW_INDEX;
2380 if (option (OPTRESOLVE)) {
2382 rc = OP_MAIN_NEXT_UNDELETED;
2387 CHECK_MODE (IsHeader (extra));
2390 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2392 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2393 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2394 redraw = REDRAW_STATUS | REDRAW_INDEX;
2395 if (option (OPTRESOLVE)) {
2401 case OP_UNDELETE_THREAD:
2402 case OP_UNDELETE_SUBTHREAD:
2403 CHECK_MODE (IsHeader (extra));
2406 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2408 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2409 ch == OP_UNDELETE_THREAD ? 0 : 1)
2410 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2411 ch == OP_UNDELETE_THREAD ? 0 : 1);
2414 if (option (OPTRESOLVE)) {
2415 rc = (ch == OP_DELETE_THREAD) ?
2416 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2420 if (!option (OPTRESOLVE) && PagerIndexLines)
2421 redraw = REDRAW_FULL;
2423 redraw = REDRAW_STATUS | REDRAW_INDEX;
2432 if (option (OPTFORCEBUFFYCHECK))
2435 redraw |= REDRAW_SIDEBAR;
2438 case OP_VIEW_ATTACHMENTS:
2439 if (flags & M_PAGER_ATTACHMENT) {
2441 rc = OP_ATTACH_COLLAPSE;
2444 CHECK_MODE (IsHeader (extra));
2445 mutt_view_attachments (extra->hdr);
2446 if (extra->hdr->attach_del)
2447 Context->changed = 1;
2448 redraw = REDRAW_FULL;
2451 case OP_EXTRACT_KEYS:
2452 CHECK_MODE (IsHeader (extra));
2453 crypt_extract_keys_from_messages (extra->hdr);
2454 redraw = REDRAW_FULL;
2457 case OP_SIDEBAR_SCROLL_UP:
2458 case OP_SIDEBAR_SCROLL_DOWN:
2459 case OP_SIDEBAR_NEXT:
2460 case OP_SIDEBAR_NEXT_NEW:
2461 case OP_SIDEBAR_PREV:
2462 case OP_SIDEBAR_PREV_NEW:
2463 sidebar_scroll (ch);
2472 if (IsHeader (extra)) {
2473 Context->msgnotreadyet = -1;
2478 OldHdr = extra->hdr;
2482 cleanup_quote(&QuoteList);
2484 for (i = 0; i < maxLine; i++) {
2485 p_delete(&lineInfo[i].syntax);
2487 p_delete(&lineInfo[i].search);
2489 if (SearchCompiled) {
2490 regfree (&SearchRE);
2493 p_delete(&lineInfo);
2495 mutt_menuDestroy (&pager_index);
2496 return (rc != -1 ? rc : 0);
2500 #undef CHECK_READONLY