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 /* This pager is actually not so simple as it once was. It now operates in
1284 two modes: one for viewing messages and the other for viewing help. These
1285 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1286 is there so that we can do operations on the current message without the
1287 need to pop back out to the main-menu. */
1289 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1291 static char searchbuf[STRING];
1292 char buffer[LONG_STRING];
1293 int maxLine, lastLine = 0;
1294 struct line_t *lineInfo;
1295 struct q_class_t *QuoteList = NULL;
1296 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1297 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1299 int redraw = REDRAW_FULL;
1301 off_t last_pos = 0, last_offset = 0;
1302 int old_smart_wrap, old_markers;
1305 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1306 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1308 int bodyoffset = 1; /* offset of first line of real text */
1309 int statusoffset = 0; /* offset for the status bar */
1310 int helpoffset = LINES - 2; /* offset for the help bar. */
1311 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1313 MUTTMENU *pager_index = NULL; /* the Pager Index (PI) */
1314 int indexoffset = 0; /* offset for the PI */
1315 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1316 int indicator = indexlen / 3; /* the indicator line of the PI */
1317 int old_PagerIndexLines; /* some people want to resize it
1318 * while inside the pager... */
1324 if (!(flags & M_SHOWCOLOR))
1325 flags |= M_SHOWFLAT;
1327 if ((fp = fopen (fname, "r")) == NULL) {
1328 mutt_perror (fname);
1332 if (stat (fname, &sb) != 0) {
1333 mutt_perror (fname);
1339 /* Initialize variables */
1341 if (IsHeader (extra) && !extra->hdr->read) {
1342 Context->msgnotreadyet = extra->hdr->msgno;
1343 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1346 lineInfo = p_new(struct line_t, maxLine = LINES);
1347 for (i = 0; i < maxLine; i++) {
1348 p_clear(&lineInfo[i], 1);
1349 lineInfo[i].type = -1;
1350 lineInfo[i].search_cnt = -1;
1351 lineInfo[i].syntax = p_new(struct syntax_t, 1);
1352 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1359 if (redraw & REDRAW_FULL) {
1360 SETCOLOR (MT_COLOR_NORMAL);
1361 /* clear() doesn't optimize screen redraws */
1365 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1366 indexlen = Context->vcount + 1;
1368 indexlen = PagerIndexLines;
1370 indicator = indexlen / 3;
1372 if (option (OPTSTATUSONTOP)) {
1374 statusoffset = IsHeader (extra) ? indexlen : 0;
1375 bodyoffset = statusoffset + 1;
1376 helpoffset = LINES - 2;
1377 bodylen = helpoffset - bodyoffset;
1378 if (!option (OPTHELP))
1383 statusoffset = LINES - 2;
1384 if (!option (OPTHELP))
1386 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1387 bodylen = statusoffset - bodyoffset;
1390 if (option (OPTHELP)) {
1391 SETCOLOR (MT_COLOR_STATUS);
1392 move (helpoffset, SW);
1393 mutt_paddstr (COLS-SW, "");
1394 SETCOLOR (MT_COLOR_NORMAL);
1397 if (Resize != NULL) {
1398 if ((SearchCompiled = Resize->SearchCompiled)) {
1400 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1401 SearchFlag = M_SEARCH;
1402 SearchBack = Resize->SearchBack;
1404 lines = Resize->line;
1405 redraw |= REDRAW_SIGWINCH;
1410 if (IsHeader (extra) && PagerIndexLines) {
1411 if (pager_index == NULL) {
1412 /* only allocate the space if/when we need the index.
1413 Initialise the menu as per the main index */
1414 pager_index = mutt_new_menu ();
1415 pager_index->menu = MENU_MAIN;
1416 pager_index->make_entry = index_make_entry;
1417 pager_index->color = index_color;
1418 pager_index->max = Context->vcount;
1419 pager_index->current = extra->hdr->virtual;
1422 SETCOLOR (MT_COLOR_NORMAL);
1423 pager_index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1425 pager_index->pagelen = indexlen - 1;
1427 /* some fudge to work out where abouts the indicator should go */
1428 if (pager_index->current - indicator < 0)
1429 pager_index->top = 0;
1430 else if (pager_index->max - pager_index->current < pager_index->pagelen - indicator)
1431 pager_index->top = pager_index->max - pager_index->pagelen;
1433 pager_index->top = pager_index->current - indicator;
1435 menu_redraw_index (pager_index);
1438 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1442 if (redraw & REDRAW_SIGWINCH) {
1445 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1446 has_types | SearchFlag, &QuoteList, &q_level,
1447 &force_redraw, &SearchRE) == 0) {
1448 if (!lineInfo[i].continuation && ++j == lines) {
1453 redraw |= REDRAW_SIDEBAR;
1457 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1459 move (bodyoffset, SW);
1460 curline = oldtopline = topline;
1464 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1465 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1467 (flags & M_DISPLAYFLAGS) | hideQuoted |
1468 SearchFlag, &QuoteList, &q_level, &force_redraw,
1472 move (lines + bodyoffset, SW);
1473 redraw |= REDRAW_SIDEBAR;
1475 last_offset = lineInfo[curline].offset;
1476 } while (force_redraw);
1478 SETCOLOR (MT_COLOR_TILDE);
1479 BKGDSET (MT_COLOR_TILDE);
1480 while (lines < bodylen) {
1482 if (option (OPTTILDE))
1486 move (lines + bodyoffset, SW);
1488 /* We are going to update the pager status bar, so it isn't
1489 * necessary to reset to normal color now. */
1491 redraw |= REDRAW_STATUS; /* need to update the % seen */
1494 if (redraw & REDRAW_STATUS) {
1495 /* print out the pager status bar */
1496 SETCOLOR (MT_COLOR_STATUS);
1497 BKGDSET (MT_COLOR_STATUS);
1498 CLEARLINE_WIN (statusoffset);
1499 if (IsHeader (extra)) {
1500 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1501 size_t l2 = sizeof (buffer);
1503 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1504 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1506 else if (IsMsgAttach (extra)) {
1507 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1508 size_t l2 = sizeof (buffer);
1510 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1511 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1513 move(statusoffset,SW);
1514 mutt_paddstr (COLS - 10 - SW, IsHeader (extra) ||
1515 IsMsgAttach (extra) ? buffer : banner);
1518 if (last_pos < sb.st_size - 1)
1519 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1521 addstr (topline == 0 ? "all)" : "end)");
1522 BKGDSET (MT_COLOR_NORMAL);
1523 SETCOLOR (MT_COLOR_NORMAL);
1526 if (redraw & REDRAW_SIDEBAR)
1529 if ((redraw & REDRAW_INDEX) && pager_index) {
1530 /* redraw the pager_index indicator, because the
1531 * flags for this message might have changed. */
1532 menu_redraw_current (pager_index);
1534 /* print out the pager_index status bar */
1535 menu_status_line (buffer, sizeof (buffer), pager_index, NONULL (Status));
1536 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), SW);
1537 SETCOLOR (MT_COLOR_STATUS);
1538 BKGDSET (MT_COLOR_STATUS);
1539 mutt_paddstr (COLS-SW, buffer);
1540 SETCOLOR (MT_COLOR_NORMAL);
1541 BKGDSET (MT_COLOR_NORMAL);
1543 /* if we're not using the index, update every time */
1549 if (option(OPTBRAILLEFRIENDLY)) {
1550 if (brailleLine!=-1) {
1551 move(brailleLine+1, 0);
1555 move (statusoffset, COLS-1);
1558 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1559 && lineInfo[curline].offset < sb.st_size-1) {
1560 if (TopLine - topline > lines)
1569 ch = km_dokey (MENU_PAGER);
1571 mutt_clear_error ();
1578 else if (SigWinch) {
1579 mutt_resize_screen ();
1581 /* Store current position. */
1583 for (i = 0; i <= topline; i++)
1584 if (!lineInfo[i].continuation)
1587 if (flags & M_PAGER_RETWINCH) {
1588 Resize = p_new(struct resize, 1);
1590 Resize->line = lines;
1591 Resize->SearchCompiled = SearchCompiled;
1592 Resize->SearchBack = SearchBack;
1595 rc = OP_REFORMAT_WINCH;
1597 for (i = 0; i < maxLine; i++) {
1598 lineInfo[i].offset = 0;
1599 lineInfo[i].type = -1;
1600 lineInfo[i].continuation = 0;
1601 lineInfo[i].chunks = 0;
1602 lineInfo[i].search_cnt = -1;
1603 lineInfo[i].quote = NULL;
1605 p_realloc(&lineInfo[i].syntax, 1);
1607 p_delete(&lineInfo[i].search);
1613 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1618 clearok (stdscr, TRUE); /*force complete redraw */
1621 else if (ch == -1) {
1635 if (lineInfo[curline].offset < sb.st_size - 1) {
1636 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1638 else if (option (OPTPAGERSTOP)) {
1639 /* emulate "less -q" and don't go on to the next message. */
1640 mutt_error _("Bottom of message is shown.");
1642 /* end of the current message, so display the next message. */
1643 rc = OP_MAIN_NEXT_UNDELETED;
1651 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1654 mutt_error _("Top of message is shown.");
1658 if (lineInfo[curline].offset < sb.st_size - 1) {
1661 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1667 mutt_error _("Bottom of message is shown.");
1672 topline = upNLines (1, lineInfo, topline, hideQuoted);
1674 mutt_error _("Top of message is shown.");
1681 mutt_error _("Top of message is shown.");
1686 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1688 mutt_error _("Top of message is shown.");
1692 if (lineInfo[curline].offset < sb.st_size - 1) {
1693 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1695 else if (option (OPTPAGERSTOP)) {
1696 /* emulate "less -q" and don't go on to the next message. */
1697 mutt_error _("Bottom of message is shown.");
1699 /* end of the current message, so display the next message. */
1700 rc = OP_MAIN_NEXT_UNDELETED;
1705 case OP_SEARCH_NEXT:
1706 case OP_SEARCH_OPPOSITE:
1707 if (SearchCompiled) {
1709 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1710 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1711 /* searching forward */
1712 for (i = topline + 1; i < lastLine; i++) {
1713 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1714 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1721 mutt_error _("Not found.");
1724 /* searching backward */
1725 for (i = topline - 1; i >= 0; i--) {
1726 if ((!hideQuoted || (has_types &&
1727 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1728 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1735 mutt_error _("Not found.");
1738 if (lineInfo[topline].search_cnt > 0)
1739 SearchFlag = M_SEARCH;
1743 /* no previous search pattern, so fall through to search */
1746 case OP_SEARCH_REVERSE:
1747 m_strcpy(buffer, sizeof(buffer), searchbuf);
1748 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1749 _("Search: ")), buffer, sizeof (buffer),
1753 if (!m_strcmp (buffer, searchbuf)) {
1754 if (SearchCompiled) {
1755 /* do an implicit search-next */
1756 if (ch == OP_SEARCH)
1757 ch = OP_SEARCH_NEXT;
1759 ch = OP_SEARCH_OPPOSITE;
1768 m_strcpy(searchbuf, sizeof(searchbuf), buffer);
1770 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1771 if (ch == OP_SEARCH)
1773 else if (ch == OP_SEARCH_REVERSE)
1776 if (SearchCompiled) {
1777 regfree (&SearchRE);
1778 for (i = 0; i < lastLine; i++) {
1779 p_delete(&lineInfo[i].search);
1780 lineInfo[i].search_cnt = -1;
1785 REGCOMP (&SearchRE, searchbuf,
1786 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1787 regerror (err, &SearchRE, buffer, sizeof (buffer));
1788 mutt_error ("%s", buffer);
1789 regfree (&SearchRE);
1790 for (i = 0; i < maxLine; i++) {
1792 p_delete(&lineInfo[i].search);
1793 lineInfo[i].search_cnt = -1;
1800 /* update the search pointers */
1802 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1803 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1804 &QuoteList, &q_level,
1805 &force_redraw, &SearchRE) == 0) {
1807 redraw |= REDRAW_SIDEBAR;
1811 /* searching forward */
1812 for (i = topline; i < lastLine; i++) {
1813 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1814 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1822 /* searching backward */
1823 for (i = topline; i >= 0; i--) {
1824 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1825 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1833 if (lineInfo[topline].search_cnt == 0) {
1835 mutt_error _("Not found.");
1838 SearchFlag = M_SEARCH;
1840 redraw = REDRAW_BODY;
1843 case OP_SEARCH_TOGGLE:
1844 if (SearchCompiled) {
1845 SearchFlag ^= M_SEARCH;
1846 redraw = REDRAW_BODY;
1851 /* don't let the user enter the help-menu from the help screen! */
1854 mutt_help (MENU_PAGER);
1855 redraw = REDRAW_FULL;
1859 mutt_error _("Help is currently being shown.");
1862 case OP_PAGER_HIDE_QUOTED:
1864 hideQuoted ^= M_HIDE;
1865 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1866 topline = upNLines (1, lineInfo, topline, hideQuoted);
1868 redraw = REDRAW_BODY;
1872 case OP_PAGER_SKIP_QUOTED:
1875 int new_topline = topline;
1877 while ((new_topline < lastLine ||
1878 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1879 new_topline, &lastLine,
1880 &maxLine, M_TYPES, &QuoteList,
1881 &q_level, &force_redraw,
1883 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
1884 redraw |= REDRAW_SIDEBAR;
1889 mutt_error _("No more quoted text.");
1894 while ((new_topline < lastLine ||
1895 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1896 new_topline, &lastLine,
1897 &maxLine, M_TYPES, &QuoteList,
1898 &q_level, &force_redraw,
1900 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
1902 redraw |= REDRAW_SIDEBAR;
1906 mutt_error _("No more unquoted text after quoted text.");
1910 topline = new_topline;
1914 case OP_PAGER_BOTTOM: /* move to the end of the file */
1915 if (lineInfo[curline].offset < sb.st_size - 1) {
1917 /* make sure the types are defined to the end of file */
1918 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1919 &maxLine, has_types,
1920 &QuoteList, &q_level, &force_redraw,
1923 redraw |= REDRAW_SIDEBAR;
1925 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
1928 mutt_error _("Bottom of message is shown.");
1932 clearok (stdscr, TRUE);
1933 redraw = REDRAW_FULL;
1937 km_error_key (MENU_PAGER);
1940 /* --------------------------------------------------------------------
1941 * The following are operations on the current message rather than
1942 * adjusting the view of the message.
1945 case OP_BOUNCE_MESSAGE:
1946 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
1948 if (IsMsgAttach (extra))
1949 mutt_attach_bounce (extra->fp, extra->hdr,
1950 extra->idx, extra->idxlen, extra->bdy);
1952 ci_bounce_message (extra->hdr, &redraw);
1956 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
1958 if (IsMsgAttach (extra))
1959 mutt_attach_resend (extra->fp, extra->hdr,
1960 extra->idx, extra->idxlen, extra->bdy);
1962 mutt_resend_message (NULL, extra->ctx, extra->hdr);
1963 redraw = REDRAW_FULL;
1966 case OP_CHECK_TRADITIONAL:
1967 CHECK_MODE (IsHeader (extra));
1968 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
1970 rc = OP_CHECK_TRADITIONAL;
1974 case OP_CREATE_ALIAS:
1975 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
1976 if (IsMsgAttach (extra))
1977 mutt_create_alias (extra->bdy->hdr->env, NULL);
1979 mutt_create_alias (extra->hdr->env, NULL);
1980 MAYBE_REDRAW (redraw);
1983 case OP_PURGE_MESSAGE:
1985 CHECK_MODE (IsHeader (extra));
1988 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
1990 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
1991 mutt_set_flag (Context, extra->hdr, M_PURGED,
1992 ch != OP_PURGE_MESSAGE ? 0 : 1);
1993 if (option (OPTDELETEUNTAG))
1994 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
1995 redraw = REDRAW_STATUS | REDRAW_INDEX;
1996 if (option (OPTRESOLVE)) {
1998 rc = OP_MAIN_NEXT_UNDELETED;
2002 case OP_DELETE_THREAD:
2003 case OP_DELETE_SUBTHREAD:
2004 CHECK_MODE (IsHeader (extra));
2007 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2009 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2010 ch == OP_DELETE_THREAD ? 0 : 1);
2013 if (option (OPTDELETEUNTAG))
2014 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2015 ch == OP_DELETE_THREAD ? 0 : 1);
2016 if (option (OPTRESOLVE)) {
2017 rc = OP_MAIN_NEXT_UNDELETED;
2021 if (!option (OPTRESOLVE) && PagerIndexLines)
2022 redraw = REDRAW_FULL;
2024 redraw = REDRAW_STATUS | REDRAW_INDEX;
2028 case OP_DISPLAY_ADDRESS:
2029 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2030 if (IsMsgAttach (extra))
2031 mutt_display_address (extra->bdy->hdr->env);
2033 mutt_display_address (extra->hdr->env);
2036 case OP_ENTER_COMMAND:
2037 old_smart_wrap = option (OPTWRAP);
2038 old_markers = option (OPTMARKERS);
2039 old_PagerIndexLines = PagerIndexLines;
2041 CurrentMenu = MENU_PAGER;
2042 mutt_enter_command ();
2044 if (option (OPTNEEDRESORT)) {
2045 unset_option (OPTNEEDRESORT);
2046 CHECK_MODE (IsHeader (extra));
2047 set_option (OPTNEEDRESORT);
2050 if (old_PagerIndexLines != PagerIndexLines) {
2052 mutt_menuDestroy (&pager_index);
2056 if (option (OPTWRAP) != old_smart_wrap ||
2057 option (OPTMARKERS) != old_markers) {
2058 if (flags & M_PAGER_RETWINCH) {
2060 rc = OP_REFORMAT_WINCH;
2064 /* count the real lines above */
2066 for (i = 0; i <= topline; i++) {
2067 if (!lineInfo[i].continuation)
2071 /* we need to restart the whole thing */
2072 for (i = 0; i < maxLine; i++) {
2073 lineInfo[i].offset = 0;
2074 lineInfo[i].type = -1;
2075 lineInfo[i].continuation = 0;
2076 lineInfo[i].chunks = 0;
2077 lineInfo[i].search_cnt = -1;
2078 lineInfo[i].quote = NULL;
2080 p_realloc(&(lineInfo[i].syntax), 1);
2082 p_delete(&lineInfo[i].search);
2085 if (SearchCompiled) {
2086 regfree (&SearchRE);
2091 /* try to keep the old position */
2094 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2095 &lastLine, &maxLine,
2096 (has_types ? M_TYPES : 0),
2097 &QuoteList, &q_level, &force_redraw,
2099 redraw |= REDRAW_SIDEBAR;
2100 if (!lineInfo[topline].continuation)
2109 if (option (OPTFORCEREDRAWPAGER))
2110 redraw = REDRAW_FULL;
2111 unset_option (OPTFORCEREDRAWINDEX);
2112 unset_option (OPTFORCEREDRAWPAGER);
2115 case OP_FLAG_MESSAGE:
2116 CHECK_MODE (IsHeader (extra));
2119 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2121 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2122 redraw = REDRAW_STATUS | REDRAW_INDEX;
2123 if (option (OPTRESOLVE)) {
2125 rc = OP_MAIN_NEXT_UNDELETED;
2130 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2131 if (IsAttach (extra))
2132 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2134 mutt_pipe_message (extra->hdr);
2135 MAYBE_REDRAW (redraw);
2139 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2140 if (IsAttach (extra))
2141 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2143 mutt_print_message (extra->hdr);
2147 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2149 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2150 redraw = REDRAW_FULL;
2155 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2157 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2158 !((nntp_data_t *) extra->ctx->data)->allowed &&
2159 query_quadoption (OPT_TOMODERATED,
2161 ("Posting to this group not allowed, may be moderated. Continue?"))
2164 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2165 redraw = REDRAW_FULL;
2168 case OP_FORWARD_TO_GROUP:
2169 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2171 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2172 !((nntp_data_t *) extra->ctx->data)->allowed &&
2173 query_quadoption (OPT_TOMODERATED,
2175 ("Posting to this group not allowed, may be moderated. Continue?"))
2178 if (IsMsgAttach (extra))
2179 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2180 extra->idxlen, extra->bdy, SENDNEWS);
2182 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2184 redraw = REDRAW_FULL;
2188 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2191 if (IsMsgAttach (extra))
2192 followup_to = extra->bdy->hdr->env->followup_to;
2194 followup_to = extra->hdr->env->followup_to;
2196 if (!followup_to || m_strcasecmp(followup_to, "poster") ||
2197 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2198 _("Reply by mail as poster prefers?")) != M_YES) {
2199 if (extra->ctx && extra->ctx->magic == M_NNTP
2200 && !((nntp_data_t *) extra->ctx->data)->allowed
2201 && query_quadoption (OPT_TOMODERATED,
2203 ("Posting to this group not allowed, may be moderated. Continue?"))
2206 if (IsMsgAttach (extra))
2207 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2208 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2210 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2211 extra->ctx, extra->hdr);
2212 redraw = REDRAW_FULL;
2218 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2220 if (IsMsgAttach (extra))
2221 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2222 extra->idxlen, extra->bdy, SENDREPLY);
2224 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2225 redraw = REDRAW_FULL;
2228 case OP_RECALL_MESSAGE:
2229 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2231 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2232 redraw = REDRAW_FULL;
2235 case OP_GROUP_REPLY:
2236 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2238 if (IsMsgAttach (extra))
2239 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2240 extra->idxlen, extra->bdy,
2241 SENDREPLY | SENDGROUPREPLY);
2243 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2245 redraw = REDRAW_FULL;
2249 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2251 if (IsMsgAttach (extra))
2252 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2253 extra->idxlen, extra->bdy,
2254 SENDREPLY | SENDLISTREPLY);
2256 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2258 redraw = REDRAW_FULL;
2261 case OP_FORWARD_MESSAGE:
2262 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2264 if (IsMsgAttach (extra))
2265 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2266 extra->idxlen, extra->bdy, 0);
2268 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2269 redraw = REDRAW_FULL;
2272 case OP_DECRYPT_SAVE:
2274 if (IsAttach (extra)) {
2275 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2280 case OP_COPY_MESSAGE:
2281 case OP_DECODE_SAVE:
2282 case OP_DECODE_COPY:
2283 case OP_DECRYPT_COPY:
2284 CHECK_MODE (IsHeader (extra));
2285 if (mutt_save_message (extra->hdr,
2286 (ch == OP_DECRYPT_SAVE) ||
2287 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2288 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2289 (ch == OP_DECRYPT_SAVE)
2290 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2291 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2292 || ch == OP_DECRYPT_SAVE)) {
2293 if (option (OPTRESOLVE)) {
2295 rc = OP_MAIN_NEXT_UNDELETED;
2298 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2300 MAYBE_REDRAW (redraw);
2303 case OP_SHELL_ESCAPE:
2304 mutt_shell_escape ();
2305 MAYBE_REDRAW (redraw);
2309 CHECK_MODE (IsHeader (extra));
2310 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2312 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2313 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2314 ? NULL : Context->last_tag);
2316 redraw = REDRAW_STATUS | REDRAW_INDEX;
2317 if (option (OPTRESOLVE)) {
2324 CHECK_MODE (IsHeader (extra));
2327 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2329 if (extra->hdr->read || extra->hdr->old)
2330 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2332 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2334 Context->msgnotreadyet = -1;
2335 redraw = REDRAW_STATUS | REDRAW_INDEX;
2336 if (option (OPTRESOLVE)) {
2338 rc = OP_MAIN_NEXT_UNDELETED;
2343 CHECK_MODE (IsHeader (extra));
2346 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2348 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2349 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2350 redraw = REDRAW_STATUS | REDRAW_INDEX;
2351 if (option (OPTRESOLVE)) {
2357 case OP_UNDELETE_THREAD:
2358 case OP_UNDELETE_SUBTHREAD:
2359 CHECK_MODE (IsHeader (extra));
2362 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2364 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2365 ch == OP_UNDELETE_THREAD ? 0 : 1)
2366 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2367 ch == OP_UNDELETE_THREAD ? 0 : 1);
2370 if (option (OPTRESOLVE)) {
2371 rc = (ch == OP_DELETE_THREAD) ?
2372 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2376 if (!option (OPTRESOLVE) && PagerIndexLines)
2377 redraw = REDRAW_FULL;
2379 redraw = REDRAW_STATUS | REDRAW_INDEX;
2388 if (option (OPTFORCEBUFFYCHECK))
2391 redraw |= REDRAW_SIDEBAR;
2394 case OP_VIEW_ATTACHMENTS:
2395 if (flags & M_PAGER_ATTACHMENT) {
2397 rc = OP_ATTACH_COLLAPSE;
2400 CHECK_MODE (IsHeader (extra));
2401 mutt_view_attachments (extra->hdr);
2402 if (extra->hdr->attach_del)
2403 Context->changed = 1;
2404 redraw = REDRAW_FULL;
2407 case OP_EXTRACT_KEYS:
2408 CHECK_MODE (IsHeader (extra));
2409 crypt_extract_keys_from_messages (extra->hdr);
2410 redraw = REDRAW_FULL;
2413 case OP_SIDEBAR_SCROLL_UP:
2414 case OP_SIDEBAR_SCROLL_DOWN:
2415 case OP_SIDEBAR_NEXT:
2416 case OP_SIDEBAR_NEXT_NEW:
2417 case OP_SIDEBAR_PREV:
2418 case OP_SIDEBAR_PREV_NEW:
2419 sidebar_scroll (ch);
2428 if (IsHeader (extra)) {
2429 Context->msgnotreadyet = -1;
2434 OldHdr = extra->hdr;
2438 cleanup_quote(&QuoteList);
2440 for (i = 0; i < maxLine; i++) {
2441 p_delete(&lineInfo[i].syntax);
2443 p_delete(&lineInfo[i].search);
2445 if (SearchCompiled) {
2446 regfree (&SearchRE);
2449 p_delete(&lineInfo);
2451 mutt_menuDestroy (&pager_index);
2452 return (rc != -1 ? rc : 0);
2456 #undef CHECK_READONLY