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 #if defined (HAVE_RESIZETERM)
112 static struct resize {
119 #define NumSigLines 4
121 static int check_sig(const char *s, struct line_t *info, int n)
125 while (n > 0 && count <= NumSigLines) {
126 if (info[n].type != MT_COLOR_SIGNATURE)
135 if (count > NumSigLines) {
136 /* check for a blank line */
145 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
146 int special, ansi_attr * a)
148 int def_color; /* color without syntax hilight */
149 int color; /* final color */
150 static int last_color; /* last color set */
151 int search = 0, i, m;
154 last_color = -1; /* force attrset() */
156 if (lineInfo[n].continuation) {
157 if (!cnt && option (OPTMARKERS)) {
158 SETCOLOR (MT_COLOR_MARKERS);
160 last_color = ColorDefs[MT_COLOR_MARKERS];
162 m = (lineInfo[n].syntax)[0].first;
163 cnt += (lineInfo[n].syntax)[0].last;
168 if (!(flags & M_SHOWCOLOR))
169 def_color = ColorDefs[MT_COLOR_NORMAL];
170 else if (lineInfo[m].type == MT_COLOR_HEADER)
171 def_color = (lineInfo[m].syntax)[0].color;
173 def_color = ColorDefs[lineInfo[m].type];
175 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
176 struct q_class_t *class = lineInfo[m].quote;
179 def_color = class->color;
181 while (class && class->length > cnt) {
182 def_color = class->color;
189 if (flags & M_SHOWCOLOR) {
190 for (i = 0; i < lineInfo[m].chunks; i++) {
191 /* we assume the chunks are sorted */
192 if (cnt > (lineInfo[m].syntax)[i].last)
194 if (cnt < (lineInfo[m].syntax)[i].first)
196 if (cnt != (lineInfo[m].syntax)[i].last) {
197 color = (lineInfo[m].syntax)[i].color;
200 /* don't break here, as cnt might be in the next chunk as well */
204 if (flags & M_SEARCH) {
205 for (i = 0; i < lineInfo[m].search_cnt; i++) {
206 if (cnt > (lineInfo[m].search)[i].last)
208 if (cnt < (lineInfo[m].search)[i].first)
210 if (cnt != (lineInfo[m].search)[i].last) {
211 color = ColorDefs[MT_COLOR_SEARCH];
218 /* handle "special" bold & underlined characters */
219 if (special || a->attr) {
220 if ((a->attr & ANSI_COLOR)) {
222 a->pair = mutt_alloc_color (a->fg, a->bg);
224 if (a->attr & ANSI_BOLD)
227 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
228 if (ColorDefs[MT_COLOR_BOLD] && !search)
229 color = ColorDefs[MT_COLOR_BOLD];
233 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
234 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
235 color = ColorDefs[MT_COLOR_UNDERLINE];
237 color ^= A_UNDERLINE;
239 else if (a->attr & ANSI_REVERSE) {
242 else if (a->attr & ANSI_BLINK) {
245 else if (a->attr & ANSI_OFF) {
250 if (color != last_color) {
256 static void append_line (struct line_t *lineInfo, int n, int cnt)
260 lineInfo[n + 1].type = lineInfo[n].type;
261 lineInfo[n + 1].syntax[0].color = lineInfo[n].syntax[0].color;
262 lineInfo[n + 1].continuation = 1;
264 /* find the real start of the line */
265 for (m = n; m >= 0; m--)
266 if (lineInfo[m].continuation == 0)
269 (lineInfo[n + 1].syntax)[0].first = m;
270 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
271 cnt + (lineInfo[n].syntax)[0].last : cnt;
274 static void new_class_color (struct q_class_t *class, int *q_level)
276 class->index = (*q_level)++;
277 class->color = ColorQuote[class->index % ColorQuoteUsed];
281 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
282 int lindex, int *q_level)
284 struct q_class_t *q_list;
287 new_class->index = -1;
290 if (q_list->index >= lindex) {
292 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
295 q_list = q_list->down;
296 else if (q_list->next)
297 q_list = q_list->next;
299 while (!q_list->next) {
305 q_list = q_list->next;
309 new_class->index = lindex;
310 new_class->color = ColorQuote[lindex % ColorQuoteUsed];
314 static void cleanup_quote(struct q_class_t **QuoteList)
317 struct q_class_t *ptr = (*QuoteList)->next;
318 cleanup_quote(&(*QuoteList)->down);
319 p_delete(&(*QuoteList)->prefix);
325 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
326 const char *qptr, int length,
327 int *force_redraw, int *q_level)
329 struct q_class_t *q_list = *QuoteList;
330 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
332 int offset, tail_lng;
335 if (ColorQuoteUsed <= 1) {
336 /* not much point in classifying quotes... */
338 if (*QuoteList == NULL) {
339 class = p_new(struct q_class_t, 1);
340 class->color = ColorQuote[0];
346 /* Did I mention how much I like emulating Lisp in C? */
348 /* classify quoting prefix */
350 if (length <= q_list->length) {
351 /* case 1: check the top level nodes */
353 if (m_strncmp(qptr, q_list->prefix, length) == 0) {
354 if (length == q_list->length)
355 return q_list; /* same prefix: return the current class */
357 /* found shorter prefix */
359 /* add a node above q_list */
360 tmp = p_new(struct q_class_t, 1);
361 tmp->prefix = p_dupstr(qptr, length);
362 tmp->length = length;
364 /* replace q_list by tmp in the top level list */
366 tmp->next = q_list->next;
367 q_list->next->prev = tmp;
370 tmp->prev = q_list->prev;
371 q_list->prev->next = tmp;
374 /* make q_list a child of tmp */
378 /* q_list has no siblings for now */
382 /* update the root if necessary */
383 if (q_list == *QuoteList)
386 lindex = q_list->index;
388 /* tmp should be the return class too */
391 /* next class to test; if tmp is a shorter prefix for another
392 * node, that node can only be in the top level list, so don't
393 * go down after this point
397 /* found another branch for which tmp is a shorter prefix */
399 /* save the next sibling for later */
402 /* unlink q_list from the top level list */
404 q_list->next->prev = q_list->prev;
406 q_list->prev->next = q_list->next;
408 /* at this point, we have a tmp->down; link q_list to it */
410 /* sibling order is important here, q_list should be linked last */
418 lindex = q_list->index;
420 /* next class to test; as above, we shouldn't go down */
424 /* we found a shorter prefix, so certain quotes have changed classes */
428 /* shorter, but not a substring of the current class: try next */
429 q_list = q_list->next;
433 /* case 2: try subclassing the current top level node */
435 /* tmp != NULL means we already found a shorter prefix at case 1 */
437 && m_strncmp(qptr, q_list->prefix, q_list->length) == 0) {
438 /* ok, it's a subclass somewhere on this branch */
441 offset = q_list->length;
443 q_list = q_list->down;
444 tail_lng = length - offset;
445 tail_qptr = (char *) qptr + offset;
448 if (length <= q_list->length) {
449 if (m_strncmp(tail_qptr, (q_list->prefix) + offset, tail_lng)
451 /* same prefix: return the current class */
452 if (length == q_list->length)
455 /* found shorter common prefix */
457 /* add a node above q_list */
458 tmp = p_new(struct q_class_t, 1);
459 tmp->prefix = p_dupstr(qptr, length);
460 tmp->length = length;
462 /* replace q_list by tmp */
464 tmp->next = q_list->next;
465 q_list->next->prev = tmp;
468 tmp->prev = q_list->prev;
469 q_list->prev->next = tmp;
472 /* make q_list a child of tmp */
474 tmp->up = q_list->up;
476 if (tmp->up->down == q_list)
479 /* q_list has no siblings */
483 lindex = q_list->index;
485 /* tmp should be the return class too */
488 /* next class to test */
491 /* found another branch for which tmp is a shorter prefix */
493 /* save the next sibling for later */
496 /* unlink q_list from the top level list */
498 q_list->next->prev = q_list->prev;
500 q_list->prev->next = q_list->next;
502 /* at this point, we have a tmp->down; link q_list to it */
511 lindex = q_list->index;
513 /* next class to test */
517 /* we found a shorter prefix, so we need a redraw */
521 q_list = q_list->next;
525 /* longer than the current prefix: try subclassing it */
527 && m_strncmp(tail_qptr, (q_list->prefix) + offset,
528 q_list->length - offset) == 0) {
529 /* still a subclass: go down one level */
531 offset = q_list->length;
533 q_list = q_list->down;
534 tail_lng = length - offset;
535 tail_qptr = (char *) qptr + offset;
539 /* nope, try the next prefix */
540 q_list = q_list->next;
546 /* still not found so far: add it as a sibling to the current node */
548 tmp = p_new(struct q_class_t, 1);
549 tmp->prefix = p_dupstr(qptr, length);
550 tmp->length = length;
553 tmp->next = ptr->down;
554 ptr->down->prev = tmp;
559 new_class_color (tmp, q_level);
564 shift_class_colors (*QuoteList, tmp, lindex, q_level);
569 /* nope, try the next prefix */
570 q_list = q_list->next;
577 /* not found so far: add it as a top level class */
578 class = p_new(struct q_class_t, 1);
579 class->prefix = p_dupstr(qptr, length);
580 class->length = length;
581 new_class_color (class, q_level);
584 class->next = *QuoteList;
585 (*QuoteList)->prev = class;
591 shift_class_colors (*QuoteList, tmp, lindex, q_level);
596 static int brailleLine = -1;
597 static int brailleCol = -1;
599 static int check_attachment_marker (char *);
602 resolve_types (char *buf, char *rawbuf, struct line_t *lineInfo, int n, int last,
603 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
606 COLOR_LINE *color_line;
607 regmatch_t pmatch[1], smatch[1];
608 int found, offset, null_rx, i;
610 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
611 if (buf[0] == '\n') {
612 lineInfo[n].type = MT_COLOR_NORMAL;
613 getyx(stdscr, brailleLine, brailleCol);
615 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
616 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
617 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
619 lineInfo[n].type = MT_COLOR_HDEFAULT;
620 color_line = ColorHdrList;
622 if (REGEXEC (&color_line->rx, buf) == 0) {
623 lineInfo[n].type = MT_COLOR_HEADER;
624 lineInfo[n].syntax[0].color = color_line->pair;
627 color_line = color_line->next;
631 else if (m_strncmp("\033[0m", rawbuf, 4) == 0) /* a little hack... */
632 lineInfo[n].type = MT_COLOR_NORMAL;
633 else if (check_attachment_marker ((char *) rawbuf) == 0)
634 lineInfo[n].type = MT_COLOR_ATTACHMENT;
635 else if (m_strcmp("-- \n", buf) == 0
636 || m_strcmp("-- \r\n", buf) == 0) {
639 lineInfo[n].type = MT_COLOR_SIGNATURE;
640 while (i < last && check_sig(buf, lineInfo, i - 1) == 0 &&
641 (lineInfo[i].type == MT_COLOR_NORMAL ||
642 lineInfo[i].type == MT_COLOR_QUOTED ||
643 lineInfo[i].type == MT_COLOR_HEADER)) {
645 if (lineInfo[i].chunks) {
646 lineInfo[i].chunks = 0;
647 p_realloc(&(lineInfo[n].syntax), 1);
649 lineInfo[i++].type = MT_COLOR_SIGNATURE;
652 else if (check_sig(buf, lineInfo, n - 1) == 0)
653 lineInfo[n].type = MT_COLOR_SIGNATURE;
654 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
655 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
656 if (smatch[0].rm_so > 0) {
659 /* hack to avoid making an extra copy of buf */
660 c = buf[smatch[0].rm_so];
661 buf[smatch[0].rm_so] = 0;
663 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
664 if (q_classify && lineInfo[n].quote == NULL)
665 lineInfo[n].quote = classify_quote (QuoteList,
666 buf + pmatch[0].rm_so,
668 pmatch[0].rm_so, force_redraw,
670 lineInfo[n].type = MT_COLOR_QUOTED;
672 lineInfo[n].type = MT_COLOR_NORMAL;
675 buf[smatch[0].rm_so] = c;
677 lineInfo[n].type = MT_COLOR_NORMAL;
680 if (q_classify && lineInfo[n].quote == NULL)
681 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
682 pmatch[0].rm_eo - pmatch[0].rm_so,
683 force_redraw, q_level);
684 lineInfo[n].type = MT_COLOR_QUOTED;
687 lineInfo[n].type = MT_COLOR_NORMAL;
691 if (lineInfo[n].type == MT_COLOR_NORMAL ||
692 lineInfo[n].type == MT_COLOR_QUOTED) {
696 lineInfo[n].chunks = 0;
703 color_line = ColorBodyList;
705 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
706 (offset ? REG_NOTBOL : 0)) == 0) {
707 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
709 if (++(lineInfo[n].chunks) > 1)
710 p_realloc(&(lineInfo[n].syntax), lineInfo[n].chunks);
712 i = lineInfo[n].chunks - 1;
713 pmatch[0].rm_so += offset;
714 pmatch[0].rm_eo += offset;
716 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
717 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
718 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
719 (lineInfo[n].syntax)[i].color = color_line->pair;
720 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
721 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
726 null_rx = 1; /* empty regexp; don't add it, but keep looking */
729 color_line = color_line->next;
733 offset++; /* avoid degenerate cases */
735 offset = lineInfo[n].syntax[i].last;
736 } while (found || null_rx);
740 static int is_ansi(unsigned char *buf)
742 while (isdigit(*buf) || *buf == ';')
744 return (*buf == 'm');
747 static int check_attachment_marker(char *p)
749 char *q = AttachmentMarker;
751 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
752 return (int)(*p - *q);
755 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
759 while (isdigit (buf[x]) || buf[x] == ';')
762 /* Character Attributes */
763 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
766 mutt_free_color (a->fg, a->bg);
773 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
774 a->attr |= ANSI_BOLD;
777 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
778 a->attr |= ANSI_UNDERLINE;
781 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
782 a->attr |= ANSI_BLINK;
785 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
786 a->attr |= ANSI_REVERSE;
789 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
794 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
796 mutt_free_color (a->fg, a->bg);
798 a->attr |= ANSI_COLOR;
799 if (buf[pos + 1] != '9')
800 a->fg = buf[pos + 1] - '0';
803 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
805 mutt_free_color (a->fg, a->bg);
807 a->attr |= ANSI_COLOR;
808 if (buf[pos + 1] != '9')
809 a->bg = buf[pos + 1] - '0';
812 while (pos < x && buf[pos] != ';')
822 /* trim tail of buf so that it contains complete multibyte characters */
823 static int trim_incomplete_mbyte(unsigned char *buf, size_t len) {
827 p_clear(&mbstate, 1);
828 for (; len > 0; buf += k, len -= k) {
829 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
832 if (k == -1 || k == 0)
841 fill_buffer (FILE * f, off_t *last_pos, off_t offset, unsigned char *buf,
842 unsigned char *fmt, ssize_t blen, int *buf_ready)
845 static int b_read = 0;
847 if (*buf_ready == 0) {
849 if (offset != *last_pos)
850 fseeko (f, offset, 0);
851 if (fgets ((char *) buf, blen - 1, f) == NULL) {
855 *last_pos = ftello (f);
856 b_read = (int) (*last_pos - offset);
859 /* incomplete mbyte characters trigger a segfault in regex processing for
860 * certain versions of glibc. Trim them if necessary. */
861 if (b_read == blen - 2)
862 b_read -= trim_incomplete_mbyte(buf, b_read);
864 /* copy "buf" to "fmt", but without bold and underline controls */
867 if (*p == '\010' && (p > buf)) {
868 if (*(p + 1) == '_') /* underline */
870 else if (*(p + 1)) { /* bold or overstrike */
871 *(fmt - 1) = *(p + 1);
877 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
878 while (*p++ != 'm') /* skip ANSI sequence */
881 else if (*p == '\033' && *(p + 1) == ']'
882 && check_attachment_marker ((char *) p) == 0) {
883 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
898 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
899 int flags, ansi_attr * pa, int cnt,
900 int *pspace, int *pvch, int *pcol, int *pspecial)
902 int space = -1; /* index of the last space or TAB */
903 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
904 int ch, vch, k, last_special = -1, special = 0, t;
908 int wrap_cols = COLS;
910 if (!(flags & (M_SHOWFLAT)))
911 wrap_cols -= WrapMargin;
917 /* FIXME: this should come from lineInfo */
918 p_clear(&mbstate, 1);
920 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
921 /* Handle ANSI sequences */
922 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
923 is_ansi (buf + ch + 2))
924 ch = grok_ansi (buf, ch + 2, pa) + 1;
926 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
927 check_attachment_marker ((char *) buf + ch) == 0) {
928 while (buf[ch++] != '\a')
933 /* is anything left to do? */
937 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
938 if (k == -2 || k == -1) {
939 if (col + 4 > wrap_cols)
943 printw ("\\%03o", buf[ch]);
950 /* Handle backspace */
957 while ((wc1 = 0, mbstate1 = mbstate,
959 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
960 &mbstate1), k1 - k > 0 && wc1 == '\b')
962 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
963 &mbstate1), k2 > 0 && iswprint(wc1))) {
965 special |= (wc == '_' && special & A_UNDERLINE)
966 ? A_UNDERLINE : A_BOLD;
968 else if (wc == '_' || wc1 == '_') {
969 special |= A_UNDERLINE;
970 wc = (wc1 == '_') ? wc : wc1;
972 /* special = 0; / * overstrike: nothing to do! */
982 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
983 special || last_special || pa->attr)) {
984 resolve_color (*lineInfo, n, vch, flags, special, pa);
985 last_special = special;
992 if (col + t > wrap_cols)
1000 else if (wc == '\t') {
1006 for (; col < t; col++)
1011 else if (wc < 0x20 || wc == 0x7f) {
1012 if (col + 2 > wrap_cols)
1016 printw ("^%c", ('@' + wc) & 0x7f);
1018 else if (wc < 0x100) {
1019 if (col + 4 > wrap_cols)
1023 printw ("\\%03o", wc);
1025 if (col + 1 > wrap_cols)
1029 addch (CharsetReplacement);
1035 *pspecial = special;
1041 * flags M_SHOWFLAT, show characters (used for displaying help)
1042 * M_SHOWCOLOR, show characters in color
1043 * otherwise don't show characters
1044 * M_HIDE, don't show quoted text
1045 * M_SEARCH, resolve search patterns
1046 * M_TYPES, compute line's type
1047 * M_PAGER_NSKIP, keeps leading whitespace
1048 * M_PAGER_MARKER, eventually show markers
1051 * -1 EOF was reached
1052 * 0 normal exit, line was not displayed
1053 * >0 normal exit, line was displayed
1056 display_line (FILE * f, off_t *last_pos, struct line_t **lineInfo, int n,
1057 int *last, int *max, int flags, struct q_class_t **QuoteList,
1058 int *q_level, int *force_redraw, regex_t * SearchRE)
1060 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1061 unsigned char *buf_ptr = buf;
1062 int ch, vch, col, cnt, b_read;
1063 int buf_ready = 0, change_last = 0;
1068 ansi_attr a = { 0, 0, 0, -1 };
1069 regmatch_t pmatch[1];
1076 if (*last == *max) {
1077 p_realloc(lineInfo, *max += LINES);
1078 for (ch = *last; ch < *max; ch++) {
1079 p_clear(&(*lineInfo)[ch], 1);
1080 (*lineInfo)[ch].type = -1;
1081 (*lineInfo)[ch].search_cnt = -1;
1082 (*lineInfo)[ch].syntax = p_new(struct syntax_t, 1);
1083 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1088 /* only do color hiliting if we are viewing a message */
1089 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1090 if ((*lineInfo)[n].type == -1) {
1091 /* determine the line class */
1093 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1100 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1101 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1103 /* avoid race condition for continuation lines when scrolling up */
1105 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1107 (*lineInfo)[m].type = (*lineInfo)[n].type;
1110 /* this also prevents searching through the hidden lines */
1111 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1112 flags = 0; /* M_NOSHOW */
1115 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1116 * don't want to compute it every time M_TYPES is set, since this
1117 * would slow down the "bottom" function unacceptably. A compromise
1118 * solution is hence to call regexec() again, just to find out the
1119 * length of the quote prefix.
1121 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1122 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1125 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1131 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1132 (*lineInfo)[n].quote = classify_quote (QuoteList,
1133 (char *) fmt + pmatch[0].rm_so,
1134 pmatch[0].rm_eo - pmatch[0].rm_so,
1135 force_redraw, q_level);
1138 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1139 && (*lineInfo)[n].search_cnt == -1) {
1141 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1149 (*lineInfo)[n].search_cnt = 0;
1151 (SearchRE, (char *) fmt + offset, 1, pmatch,
1152 (offset ? REG_NOTBOL : 0)) == 0) {
1153 if (++((*lineInfo)[n].search_cnt) > 1)
1154 p_realloc(&(*lineInfo)[n].search, (*lineInfo)[n].search_cnt);
1156 (*lineInfo)[n].search = p_new(struct syntax_t, 1);
1157 pmatch[0].rm_so += offset;
1158 pmatch[0].rm_eo += offset;
1159 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1161 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1164 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1165 offset++; /* avoid degenerate cases */
1167 offset = pmatch[0].rm_eo;
1173 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1174 /* we've already scanned this line, so just exit */
1177 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1178 /* no need to try to display this line... */
1179 return (1); /* fake display */
1182 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1183 sizeof (buf), &buf_ready)) < 0) {
1189 /* now chose a good place to break the line */
1190 cnt = format_line(lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1192 buf_ptr = buf + cnt;
1194 /* move the break point only if smart_wrap is set */
1195 if (option (OPTWRAP)) {
1197 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1198 && buf[cnt] != '\r') {
1200 /* skip trailing blanks */
1201 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1203 /* a very long word with leading spaces causes infinite wrapping */
1204 if ((!ch) && (flags & M_PAGER_NSKIP))
1205 buf_ptr = buf + cnt;
1209 buf_ptr = buf + cnt; /* a very long word... */
1212 if (!(flags & M_PAGER_NSKIP))
1213 /* skip leading blanks on the next line too */
1214 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1218 if (*buf_ptr == '\r')
1220 if (*buf_ptr == '\n')
1223 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1224 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1225 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1227 /* if we don't need to display the line we are done */
1228 if (!(flags & M_SHOW))
1231 /* display the line */
1232 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1234 /* end the last color pattern (needed by S-Lang) */
1235 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1236 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1239 * Fill the blank space at the end of the line with the prevailing color.
1240 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1241 * to make sure to reset the color *after* that
1243 if (flags & M_SHOWCOLOR) {
1244 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1245 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1246 def_color = ((*lineInfo)[m].syntax)[0].color;
1248 def_color = ColorDefs[(*lineInfo)[m].type];
1250 attrset (def_color);
1252 bkgdset (def_color | ' ');
1258 * reset the color back to normal. This *must* come after the
1259 * addch('\n'), otherwise the color for this line will not be
1260 * filled to the right margin.
1262 if (flags & M_SHOWCOLOR) {
1263 SETCOLOR (MT_COLOR_NORMAL);
1264 BKGDSET (MT_COLOR_NORMAL);
1267 /* build a return code */
1268 if (!(flags & M_SHOW))
1274 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1276 while (cur > 0 && nlines > 0) {
1278 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1285 static struct mapping_t PagerHelp[] = {
1286 {N_("Exit"), OP_EXIT},
1287 {N_("PrevPg"), OP_PREV_PAGE},
1288 {N_("NextPg"), OP_NEXT_PAGE},
1291 static struct mapping_t PagerHelpExtra[] = {
1292 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1293 {N_("Del"), OP_DELETE},
1294 {N_("Reply"), OP_REPLY},
1295 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1300 static struct mapping_t PagerNewsHelpExtra[] = {
1301 {N_("Post"), OP_POST},
1302 {N_("Followup"), OP_FOLLOWUP},
1303 {N_("Del"), OP_DELETE},
1304 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1310 /* This pager is actually not so simple as it once was. It now operates in
1311 two modes: one for viewing messages and the other for viewing help. These
1312 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1313 is there so that we can do operations on the current message without the
1314 need to pop back out to the main-menu. */
1316 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1318 static char searchbuf[STRING];
1319 char buffer[LONG_STRING];
1320 char helpstr[STRING * 2];
1321 char tmphelp[STRING * 2];
1322 int maxLine, lastLine = 0;
1323 struct line_t *lineInfo;
1324 struct q_class_t *QuoteList = NULL;
1325 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1326 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1328 int redraw = REDRAW_FULL;
1330 off_t last_pos = 0, last_offset = 0;
1331 int old_smart_wrap, old_markers;
1334 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1335 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1337 int bodyoffset = 1; /* offset of first line of real text */
1338 int statusoffset = 0; /* offset for the status bar */
1339 int helpoffset = LINES - 2; /* offset for the help bar. */
1340 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1342 MUTTMENU *pager_index = NULL; /* the Pager Index (PI) */
1343 int indexoffset = 0; /* offset for the PI */
1344 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1345 int indicator = indexlen / 3; /* the indicator line of the PI */
1346 int old_PagerIndexLines; /* some people want to resize it
1347 * while inside the pager... */
1353 if (!(flags & M_SHOWCOLOR))
1354 flags |= M_SHOWFLAT;
1356 if ((fp = fopen (fname, "r")) == NULL) {
1357 mutt_perror (fname);
1361 if (stat (fname, &sb) != 0) {
1362 mutt_perror (fname);
1368 /* Initialize variables */
1370 if (IsHeader (extra) && !extra->hdr->read) {
1371 Context->msgnotreadyet = extra->hdr->msgno;
1372 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1375 lineInfo = p_new(struct line_t, maxLine = LINES);
1376 for (i = 0; i < maxLine; i++) {
1377 p_clear(&lineInfo[i], 1);
1378 lineInfo[i].type = -1;
1379 lineInfo[i].search_cnt = -1;
1380 lineInfo[i].syntax = p_new(struct syntax_t, 1);
1381 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1384 mutt_compile_help (helpstr, sizeof(helpstr), MENU_PAGER, PagerHelp);
1385 if (IsHeader (extra)) {
1386 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1387 mutt_compile_help (buffer, sizeof(buffer), MENU_PAGER,
1390 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1393 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1396 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1397 mutt_make_help (buffer, sizeof(buffer), _("Help"), MENU_PAGER, OP_HELP);
1398 snprintf (helpstr, sizeof(helpstr), "%s %s", tmphelp, buffer);
1405 if (redraw & REDRAW_FULL) {
1406 SETCOLOR (MT_COLOR_NORMAL);
1407 /* clear() doesn't optimize screen redraws */
1411 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1412 indexlen = Context->vcount + 1;
1414 indexlen = PagerIndexLines;
1416 indicator = indexlen / 3;
1418 if (option (OPTSTATUSONTOP)) {
1420 statusoffset = IsHeader (extra) ? indexlen : 0;
1421 bodyoffset = statusoffset + 1;
1422 helpoffset = LINES - 2;
1423 bodylen = helpoffset - bodyoffset;
1424 if (!option (OPTHELP))
1429 statusoffset = LINES - 2;
1430 if (!option (OPTHELP))
1432 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1433 bodylen = statusoffset - bodyoffset;
1436 if (option (OPTHELP)) {
1437 SETCOLOR (MT_COLOR_STATUS);
1438 move (helpoffset, SW);
1439 mutt_paddstr (COLS-SW, helpstr);
1440 SETCOLOR (MT_COLOR_NORMAL);
1443 #if defined (HAVE_RESIZETERM)
1444 if (Resize != NULL) {
1445 if ((SearchCompiled = Resize->SearchCompiled)) {
1447 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1448 SearchFlag = M_SEARCH;
1449 SearchBack = Resize->SearchBack;
1451 lines = Resize->line;
1452 redraw |= REDRAW_SIGWINCH;
1458 if (IsHeader (extra) && PagerIndexLines) {
1459 if (pager_index == NULL) {
1460 /* only allocate the space if/when we need the index.
1461 Initialise the menu as per the main index */
1462 pager_index = mutt_new_menu ();
1463 pager_index->menu = MENU_MAIN;
1464 pager_index->make_entry = index_make_entry;
1465 pager_index->color = index_color;
1466 pager_index->max = Context->vcount;
1467 pager_index->current = extra->hdr->virtual;
1470 SETCOLOR (MT_COLOR_NORMAL);
1471 pager_index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1473 pager_index->pagelen = indexlen - 1;
1475 /* some fudge to work out where abouts the indicator should go */
1476 if (pager_index->current - indicator < 0)
1477 pager_index->top = 0;
1478 else if (pager_index->max - pager_index->current < pager_index->pagelen - indicator)
1479 pager_index->top = pager_index->max - pager_index->pagelen;
1481 pager_index->top = pager_index->current - indicator;
1483 menu_redraw_index (pager_index);
1486 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1490 if (redraw & REDRAW_SIGWINCH) {
1493 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1494 has_types | SearchFlag, &QuoteList, &q_level,
1495 &force_redraw, &SearchRE) == 0) {
1496 if (!lineInfo[i].continuation && ++j == lines) {
1501 redraw |= REDRAW_SIDEBAR;
1505 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1507 move (bodyoffset, SW);
1508 curline = oldtopline = topline;
1512 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1513 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1515 (flags & M_DISPLAYFLAGS) | hideQuoted |
1516 SearchFlag, &QuoteList, &q_level, &force_redraw,
1520 move (lines + bodyoffset, SW);
1521 redraw |= REDRAW_SIDEBAR;
1523 last_offset = lineInfo[curline].offset;
1524 } while (force_redraw);
1526 SETCOLOR (MT_COLOR_TILDE);
1527 BKGDSET (MT_COLOR_TILDE);
1528 while (lines < bodylen) {
1530 if (option (OPTTILDE))
1534 move (lines + bodyoffset, SW);
1536 /* We are going to update the pager status bar, so it isn't
1537 * necessary to reset to normal color now. */
1539 redraw |= REDRAW_STATUS; /* need to update the % seen */
1542 if (redraw & REDRAW_STATUS) {
1543 /* print out the pager status bar */
1544 SETCOLOR (MT_COLOR_STATUS);
1545 BKGDSET (MT_COLOR_STATUS);
1546 CLEARLINE_WIN (statusoffset);
1547 if (IsHeader (extra)) {
1548 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1549 size_t l2 = sizeof (buffer);
1551 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1552 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1554 else if (IsMsgAttach (extra)) {
1555 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1556 size_t l2 = sizeof (buffer);
1558 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1559 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1561 move(statusoffset,SW);
1562 mutt_paddstr (COLS - 10 - SW, IsHeader (extra) ||
1563 IsMsgAttach (extra) ? buffer : banner);
1566 if (last_pos < sb.st_size - 1)
1567 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1569 addstr (topline == 0 ? "all)" : "end)");
1570 BKGDSET (MT_COLOR_NORMAL);
1571 SETCOLOR (MT_COLOR_NORMAL);
1574 if (redraw & REDRAW_SIDEBAR)
1577 if ((redraw & REDRAW_INDEX) && pager_index) {
1578 /* redraw the pager_index indicator, because the
1579 * flags for this message might have changed. */
1580 menu_redraw_current (pager_index);
1582 /* print out the pager_index status bar */
1583 menu_status_line (buffer, sizeof (buffer), pager_index, NONULL (Status));
1584 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), SW);
1585 SETCOLOR (MT_COLOR_STATUS);
1586 BKGDSET (MT_COLOR_STATUS);
1587 mutt_paddstr (COLS-SW, buffer);
1588 SETCOLOR (MT_COLOR_NORMAL);
1589 BKGDSET (MT_COLOR_NORMAL);
1591 /* if we're not using the index, update every time */
1597 if (option(OPTBRAILLEFRIENDLY)) {
1598 if (brailleLine!=-1) {
1599 move(brailleLine+1, 0);
1603 move (statusoffset, COLS-1);
1606 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1607 && lineInfo[curline].offset < sb.st_size-1) {
1608 if (TopLine - topline > lines)
1617 ch = km_dokey (MENU_PAGER);
1619 mutt_clear_error ();
1626 #if defined (HAVE_RESIZETERM)
1627 else if (SigWinch) {
1628 mutt_resize_screen ();
1630 /* Store current position. */
1632 for (i = 0; i <= topline; i++)
1633 if (!lineInfo[i].continuation)
1636 if (flags & M_PAGER_RETWINCH) {
1637 Resize = p_new(struct resize, 1);
1639 Resize->line = lines;
1640 Resize->SearchCompiled = SearchCompiled;
1641 Resize->SearchBack = SearchBack;
1644 rc = OP_REFORMAT_WINCH;
1646 for (i = 0; i < maxLine; i++) {
1647 lineInfo[i].offset = 0;
1648 lineInfo[i].type = -1;
1649 lineInfo[i].continuation = 0;
1650 lineInfo[i].chunks = 0;
1651 lineInfo[i].search_cnt = -1;
1652 lineInfo[i].quote = NULL;
1654 p_realloc(&lineInfo[i].syntax, 1);
1656 p_delete(&lineInfo[i].search);
1662 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1667 clearok (stdscr, TRUE); /*force complete redraw */
1671 else if (ch == -1) {
1685 if (lineInfo[curline].offset < sb.st_size - 1) {
1686 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1688 else if (option (OPTPAGERSTOP)) {
1689 /* emulate "less -q" and don't go on to the next message. */
1690 mutt_error _("Bottom of message is shown.");
1692 /* end of the current message, so display the next message. */
1693 rc = OP_MAIN_NEXT_UNDELETED;
1701 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1704 mutt_error _("Top of message is shown.");
1708 if (lineInfo[curline].offset < sb.st_size - 1) {
1711 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1717 mutt_error _("Bottom of message is shown.");
1722 topline = upNLines (1, lineInfo, topline, hideQuoted);
1724 mutt_error _("Top of message is shown.");
1731 mutt_error _("Top of message is shown.");
1736 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1738 mutt_error _("Top of message is shown.");
1742 if (lineInfo[curline].offset < sb.st_size - 1) {
1743 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1745 else if (option (OPTPAGERSTOP)) {
1746 /* emulate "less -q" and don't go on to the next message. */
1747 mutt_error _("Bottom of message is shown.");
1749 /* end of the current message, so display the next message. */
1750 rc = OP_MAIN_NEXT_UNDELETED;
1755 case OP_SEARCH_NEXT:
1756 case OP_SEARCH_OPPOSITE:
1757 if (SearchCompiled) {
1759 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1760 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1761 /* searching forward */
1762 for (i = topline + 1; i < lastLine; i++) {
1763 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1764 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1771 mutt_error _("Not found.");
1774 /* searching backward */
1775 for (i = topline - 1; i >= 0; i--) {
1776 if ((!hideQuoted || (has_types &&
1777 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1778 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1785 mutt_error _("Not found.");
1788 if (lineInfo[topline].search_cnt > 0)
1789 SearchFlag = M_SEARCH;
1793 /* no previous search pattern, so fall through to search */
1796 case OP_SEARCH_REVERSE:
1797 m_strcpy(buffer, sizeof(buffer), searchbuf);
1798 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1799 _("Search: ")), buffer, sizeof (buffer),
1803 if (!m_strcmp (buffer, searchbuf)) {
1804 if (SearchCompiled) {
1805 /* do an implicit search-next */
1806 if (ch == OP_SEARCH)
1807 ch = OP_SEARCH_NEXT;
1809 ch = OP_SEARCH_OPPOSITE;
1818 m_strcpy(searchbuf, sizeof(searchbuf), buffer);
1820 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1821 if (ch == OP_SEARCH)
1823 else if (ch == OP_SEARCH_REVERSE)
1826 if (SearchCompiled) {
1827 regfree (&SearchRE);
1828 for (i = 0; i < lastLine; i++) {
1829 p_delete(&lineInfo[i].search);
1830 lineInfo[i].search_cnt = -1;
1835 REGCOMP (&SearchRE, searchbuf,
1836 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1837 regerror (err, &SearchRE, buffer, sizeof (buffer));
1838 mutt_error ("%s", buffer);
1839 regfree (&SearchRE);
1840 for (i = 0; i < maxLine; i++) {
1842 p_delete(&lineInfo[i].search);
1843 lineInfo[i].search_cnt = -1;
1850 /* update the search pointers */
1852 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1853 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1854 &QuoteList, &q_level,
1855 &force_redraw, &SearchRE) == 0) {
1857 redraw |= REDRAW_SIDEBAR;
1861 /* searching forward */
1862 for (i = topline; i < lastLine; i++) {
1863 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1864 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1872 /* searching backward */
1873 for (i = topline; i >= 0; i--) {
1874 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1875 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1883 if (lineInfo[topline].search_cnt == 0) {
1885 mutt_error _("Not found.");
1888 SearchFlag = M_SEARCH;
1890 redraw = REDRAW_BODY;
1893 case OP_SEARCH_TOGGLE:
1894 if (SearchCompiled) {
1895 SearchFlag ^= M_SEARCH;
1896 redraw = REDRAW_BODY;
1901 /* don't let the user enter the help-menu from the help screen! */
1904 mutt_help (MENU_PAGER);
1905 redraw = REDRAW_FULL;
1909 mutt_error _("Help is currently being shown.");
1912 case OP_PAGER_HIDE_QUOTED:
1914 hideQuoted ^= M_HIDE;
1915 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1916 topline = upNLines (1, lineInfo, topline, hideQuoted);
1918 redraw = REDRAW_BODY;
1922 case OP_PAGER_SKIP_QUOTED:
1925 int new_topline = topline;
1927 while ((new_topline < lastLine ||
1928 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1929 new_topline, &lastLine,
1930 &maxLine, M_TYPES, &QuoteList,
1931 &q_level, &force_redraw,
1933 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
1934 redraw |= REDRAW_SIDEBAR;
1939 mutt_error _("No more quoted text.");
1944 while ((new_topline < lastLine ||
1945 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1946 new_topline, &lastLine,
1947 &maxLine, M_TYPES, &QuoteList,
1948 &q_level, &force_redraw,
1950 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
1952 redraw |= REDRAW_SIDEBAR;
1956 mutt_error _("No more unquoted text after quoted text.");
1960 topline = new_topline;
1964 case OP_PAGER_BOTTOM: /* move to the end of the file */
1965 if (lineInfo[curline].offset < sb.st_size - 1) {
1967 /* make sure the types are defined to the end of file */
1968 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1969 &maxLine, has_types,
1970 &QuoteList, &q_level, &force_redraw,
1973 redraw |= REDRAW_SIDEBAR;
1975 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
1978 mutt_error _("Bottom of message is shown.");
1982 clearok (stdscr, TRUE);
1983 redraw = REDRAW_FULL;
1987 km_error_key (MENU_PAGER);
1990 /* --------------------------------------------------------------------
1991 * The following are operations on the current message rather than
1992 * adjusting the view of the message.
1995 case OP_BOUNCE_MESSAGE:
1996 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
1998 if (IsMsgAttach (extra))
1999 mutt_attach_bounce (extra->fp, extra->hdr,
2000 extra->idx, extra->idxlen, extra->bdy);
2002 ci_bounce_message (extra->hdr, &redraw);
2006 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2008 if (IsMsgAttach (extra))
2009 mutt_attach_resend (extra->fp, extra->hdr,
2010 extra->idx, extra->idxlen, extra->bdy);
2012 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2013 redraw = REDRAW_FULL;
2016 case OP_CHECK_TRADITIONAL:
2017 CHECK_MODE (IsHeader (extra));
2018 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2020 rc = OP_CHECK_TRADITIONAL;
2024 case OP_CREATE_ALIAS:
2025 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2026 if (IsMsgAttach (extra))
2027 mutt_create_alias (extra->bdy->hdr->env, NULL);
2029 mutt_create_alias (extra->hdr->env, NULL);
2030 MAYBE_REDRAW (redraw);
2033 case OP_PURGE_MESSAGE:
2035 CHECK_MODE (IsHeader (extra));
2038 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2040 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2041 mutt_set_flag (Context, extra->hdr, M_PURGED,
2042 ch != OP_PURGE_MESSAGE ? 0 : 1);
2043 if (option (OPTDELETEUNTAG))
2044 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2045 redraw = REDRAW_STATUS | REDRAW_INDEX;
2046 if (option (OPTRESOLVE)) {
2048 rc = OP_MAIN_NEXT_UNDELETED;
2052 case OP_DELETE_THREAD:
2053 case OP_DELETE_SUBTHREAD:
2054 CHECK_MODE (IsHeader (extra));
2057 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2059 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2060 ch == OP_DELETE_THREAD ? 0 : 1);
2063 if (option (OPTDELETEUNTAG))
2064 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2065 ch == OP_DELETE_THREAD ? 0 : 1);
2066 if (option (OPTRESOLVE)) {
2067 rc = OP_MAIN_NEXT_UNDELETED;
2071 if (!option (OPTRESOLVE) && PagerIndexLines)
2072 redraw = REDRAW_FULL;
2074 redraw = REDRAW_STATUS | REDRAW_INDEX;
2078 case OP_DISPLAY_ADDRESS:
2079 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2080 if (IsMsgAttach (extra))
2081 mutt_display_address (extra->bdy->hdr->env);
2083 mutt_display_address (extra->hdr->env);
2086 case OP_ENTER_COMMAND:
2087 old_smart_wrap = option (OPTWRAP);
2088 old_markers = option (OPTMARKERS);
2089 old_PagerIndexLines = PagerIndexLines;
2091 CurrentMenu = MENU_PAGER;
2092 mutt_enter_command ();
2094 if (option (OPTNEEDRESORT)) {
2095 unset_option (OPTNEEDRESORT);
2096 CHECK_MODE (IsHeader (extra));
2097 set_option (OPTNEEDRESORT);
2100 if (old_PagerIndexLines != PagerIndexLines) {
2102 mutt_menuDestroy (&pager_index);
2106 if (option (OPTWRAP) != old_smart_wrap ||
2107 option (OPTMARKERS) != old_markers) {
2108 if (flags & M_PAGER_RETWINCH) {
2110 rc = OP_REFORMAT_WINCH;
2114 /* count the real lines above */
2116 for (i = 0; i <= topline; i++) {
2117 if (!lineInfo[i].continuation)
2121 /* we need to restart the whole thing */
2122 for (i = 0; i < maxLine; i++) {
2123 lineInfo[i].offset = 0;
2124 lineInfo[i].type = -1;
2125 lineInfo[i].continuation = 0;
2126 lineInfo[i].chunks = 0;
2127 lineInfo[i].search_cnt = -1;
2128 lineInfo[i].quote = NULL;
2130 p_realloc(&(lineInfo[i].syntax), 1);
2132 p_delete(&lineInfo[i].search);
2135 if (SearchCompiled) {
2136 regfree (&SearchRE);
2141 /* try to keep the old position */
2144 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2145 &lastLine, &maxLine,
2146 (has_types ? M_TYPES : 0),
2147 &QuoteList, &q_level, &force_redraw,
2149 redraw |= REDRAW_SIDEBAR;
2150 if (!lineInfo[topline].continuation)
2159 if (option (OPTFORCEREDRAWPAGER))
2160 redraw = REDRAW_FULL;
2161 unset_option (OPTFORCEREDRAWINDEX);
2162 unset_option (OPTFORCEREDRAWPAGER);
2165 case OP_FLAG_MESSAGE:
2166 CHECK_MODE (IsHeader (extra));
2169 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2171 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2172 redraw = REDRAW_STATUS | REDRAW_INDEX;
2173 if (option (OPTRESOLVE)) {
2175 rc = OP_MAIN_NEXT_UNDELETED;
2180 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2181 if (IsAttach (extra))
2182 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2184 mutt_pipe_message (extra->hdr);
2185 MAYBE_REDRAW (redraw);
2189 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2190 if (IsAttach (extra))
2191 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2193 mutt_print_message (extra->hdr);
2197 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2199 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2200 redraw = REDRAW_FULL;
2205 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2207 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2208 !((nntp_data_t *) extra->ctx->data)->allowed &&
2209 query_quadoption (OPT_TOMODERATED,
2211 ("Posting to this group not allowed, may be moderated. Continue?"))
2214 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2215 redraw = REDRAW_FULL;
2218 case OP_FORWARD_TO_GROUP:
2219 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2221 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2222 !((nntp_data_t *) extra->ctx->data)->allowed &&
2223 query_quadoption (OPT_TOMODERATED,
2225 ("Posting to this group not allowed, may be moderated. Continue?"))
2228 if (IsMsgAttach (extra))
2229 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2230 extra->idxlen, extra->bdy, SENDNEWS);
2232 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2234 redraw = REDRAW_FULL;
2238 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2241 if (IsMsgAttach (extra))
2242 followup_to = extra->bdy->hdr->env->followup_to;
2244 followup_to = extra->hdr->env->followup_to;
2246 if (!followup_to || m_strcasecmp(followup_to, "poster") ||
2247 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2248 _("Reply by mail as poster prefers?")) != M_YES) {
2249 if (extra->ctx && extra->ctx->magic == M_NNTP
2250 && !((nntp_data_t *) extra->ctx->data)->allowed
2251 && query_quadoption (OPT_TOMODERATED,
2253 ("Posting to this group not allowed, may be moderated. Continue?"))
2256 if (IsMsgAttach (extra))
2257 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2258 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2260 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2261 extra->ctx, extra->hdr);
2262 redraw = REDRAW_FULL;
2268 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2270 if (IsMsgAttach (extra))
2271 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2272 extra->idxlen, extra->bdy, SENDREPLY);
2274 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2275 redraw = REDRAW_FULL;
2278 case OP_RECALL_MESSAGE:
2279 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2281 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2282 redraw = REDRAW_FULL;
2285 case OP_GROUP_REPLY:
2286 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2288 if (IsMsgAttach (extra))
2289 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2290 extra->idxlen, extra->bdy,
2291 SENDREPLY | SENDGROUPREPLY);
2293 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2295 redraw = REDRAW_FULL;
2299 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2301 if (IsMsgAttach (extra))
2302 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2303 extra->idxlen, extra->bdy,
2304 SENDREPLY | SENDLISTREPLY);
2306 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2308 redraw = REDRAW_FULL;
2311 case OP_FORWARD_MESSAGE:
2312 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2314 if (IsMsgAttach (extra))
2315 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2316 extra->idxlen, extra->bdy, 0);
2318 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2319 redraw = REDRAW_FULL;
2322 case OP_DECRYPT_SAVE:
2324 if (IsAttach (extra)) {
2325 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2330 case OP_COPY_MESSAGE:
2331 case OP_DECODE_SAVE:
2332 case OP_DECODE_COPY:
2333 case OP_DECRYPT_COPY:
2334 CHECK_MODE (IsHeader (extra));
2335 if (mutt_save_message (extra->hdr,
2336 (ch == OP_DECRYPT_SAVE) ||
2337 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2338 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2339 (ch == OP_DECRYPT_SAVE)
2340 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2341 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2342 || ch == OP_DECRYPT_SAVE)) {
2343 if (option (OPTRESOLVE)) {
2345 rc = OP_MAIN_NEXT_UNDELETED;
2348 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2350 MAYBE_REDRAW (redraw);
2353 case OP_SHELL_ESCAPE:
2354 mutt_shell_escape ();
2355 MAYBE_REDRAW (redraw);
2359 CHECK_MODE (IsHeader (extra));
2360 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2362 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2363 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2364 ? NULL : Context->last_tag);
2366 redraw = REDRAW_STATUS | REDRAW_INDEX;
2367 if (option (OPTRESOLVE)) {
2374 CHECK_MODE (IsHeader (extra));
2377 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2379 if (extra->hdr->read || extra->hdr->old)
2380 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2382 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2384 Context->msgnotreadyet = -1;
2385 redraw = REDRAW_STATUS | REDRAW_INDEX;
2386 if (option (OPTRESOLVE)) {
2388 rc = OP_MAIN_NEXT_UNDELETED;
2393 CHECK_MODE (IsHeader (extra));
2396 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2398 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2399 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2400 redraw = REDRAW_STATUS | REDRAW_INDEX;
2401 if (option (OPTRESOLVE)) {
2407 case OP_UNDELETE_THREAD:
2408 case OP_UNDELETE_SUBTHREAD:
2409 CHECK_MODE (IsHeader (extra));
2412 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2414 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2415 ch == OP_UNDELETE_THREAD ? 0 : 1)
2416 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2417 ch == OP_UNDELETE_THREAD ? 0 : 1);
2420 if (option (OPTRESOLVE)) {
2421 rc = (ch == OP_DELETE_THREAD) ?
2422 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2426 if (!option (OPTRESOLVE) && PagerIndexLines)
2427 redraw = REDRAW_FULL;
2429 redraw = REDRAW_STATUS | REDRAW_INDEX;
2438 if (option (OPTFORCEBUFFYCHECK))
2441 redraw |= REDRAW_SIDEBAR;
2444 case OP_VIEW_ATTACHMENTS:
2445 if (flags & M_PAGER_ATTACHMENT) {
2447 rc = OP_ATTACH_COLLAPSE;
2450 CHECK_MODE (IsHeader (extra));
2451 mutt_view_attachments (extra->hdr);
2452 if (extra->hdr->attach_del)
2453 Context->changed = 1;
2454 redraw = REDRAW_FULL;
2457 case OP_EXTRACT_KEYS:
2458 CHECK_MODE (IsHeader (extra));
2459 crypt_extract_keys_from_messages (extra->hdr);
2460 redraw = REDRAW_FULL;
2463 case OP_SIDEBAR_SCROLL_UP:
2464 case OP_SIDEBAR_SCROLL_DOWN:
2465 case OP_SIDEBAR_NEXT:
2466 case OP_SIDEBAR_NEXT_NEW:
2467 case OP_SIDEBAR_PREV:
2468 case OP_SIDEBAR_PREV_NEW:
2469 sidebar_scroll (ch);
2478 if (IsHeader (extra)) {
2479 Context->msgnotreadyet = -1;
2484 OldHdr = extra->hdr;
2488 cleanup_quote(&QuoteList);
2490 for (i = 0; i < maxLine; i++) {
2491 p_delete(&lineInfo[i].syntax);
2493 p_delete(&lineInfo[i].search);
2495 if (SearchCompiled) {
2496 regfree (&SearchRE);
2499 p_delete(&lineInfo);
2501 mutt_menuDestroy (&pager_index);
2502 return (rc != -1 ? rc : 0);
2506 #undef CHECK_READONLY