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/lib-ui.h>
17 #include <lib-ui/menu.h>
18 #include <lib-ui/madtty.h>
19 #include <lib-mx/mx.h>
28 #include "recvattach.h"
32 #include <imap/imap_private.h>
34 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
36 #define IsAttach(x) (x && (x)->bdy)
37 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
38 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
39 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
40 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
42 /* hack to return to position when returning from index to same message */
43 static int TopLine = 0;
44 static HEADER *OldHdr = NULL;
46 #define CHECK_MODE(x) \
49 mutt_error _("Not available in this menu."); \
53 #define CHECK_READONLY \
54 if (Context->readonly) { \
56 mutt_error _("Mailbox is read-only."); \
60 #define CHECK_ATTACH \
61 if (option(OPTATTACHMSG)) { \
63 mutt_error _("Function not permitted in attach-message mode."); \
72 struct q_class_t *next, *prev;
73 struct q_class_t *down, *up;
88 struct syntax_t *syntax;
89 struct syntax_t *search;
90 struct q_class_t *quote;
93 #define ANSI_OFF (1<<0)
94 #define ANSI_BLINK (1<<1)
95 #define ANSI_BOLD (1<<2)
96 #define ANSI_UNDERLINE (1<<3)
97 #define ANSI_REVERSE (1<<4)
98 #define ANSI_COLOR (1<<5)
100 typedef struct _ansi_attr {
107 static short InHelp = 0;
109 static struct resize {
115 #define NumSigLines 4
117 static int check_sig(const char *s, struct line_t *info, int n)
121 while (n > 0 && count <= NumSigLines) {
122 if (info[n].type != MT_COLOR_SIGNATURE)
131 if (count > NumSigLines) {
132 /* check for a blank line */
141 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
142 int special, ansi_attr * a)
144 int def_color; /* color without syntax hilight */
145 int color; /* final color */
146 static int last_color; /* last color set */
147 int search = 0, i, m;
150 last_color = -1; /* force wattrset(main_w) */
152 if (lineInfo[n].continuation) {
153 if (!cnt && option (OPTMARKERS)) {
154 SETCOLOR(main_w, MT_COLOR_MARKERS);
156 last_color = ColorDefs[MT_COLOR_MARKERS];
158 m = (lineInfo[n].syntax)[0].first;
159 cnt += (lineInfo[n].syntax)[0].last;
164 if (!(flags & M_SHOWCOLOR))
165 def_color = ColorDefs[MT_COLOR_NORMAL];
166 else if (lineInfo[m].type == MT_COLOR_HEADER)
167 def_color = (lineInfo[m].syntax)[0].color;
169 def_color = ColorDefs[lineInfo[m].type];
171 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
172 struct q_class_t *class = lineInfo[m].quote;
175 def_color = class->color;
177 while (class && class->length > cnt) {
178 def_color = class->color;
185 if (flags & M_SHOWCOLOR) {
186 for (i = 0; i < lineInfo[m].chunks; i++) {
187 /* we assume the chunks are sorted */
188 if (cnt > (lineInfo[m].syntax)[i].last)
190 if (cnt < (lineInfo[m].syntax)[i].first)
192 if (cnt != (lineInfo[m].syntax)[i].last) {
193 color = (lineInfo[m].syntax)[i].color;
196 /* don't break here, as cnt might be in the next chunk as well */
200 if (flags & M_SEARCH) {
201 for (i = 0; i < lineInfo[m].search_cnt; i++) {
202 if (cnt > (lineInfo[m].search)[i].last)
204 if (cnt < (lineInfo[m].search)[i].first)
206 if (cnt != (lineInfo[m].search)[i].last) {
207 color = ColorDefs[MT_COLOR_SEARCH];
214 /* handle "special" bold & underlined characters */
215 if (special || a->attr) {
216 if ((a->attr & ANSI_COLOR)) {
218 a->pair = madtty_color_pair(a->fg, a->bg);
220 if (a->attr & ANSI_BOLD)
223 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
224 if (ColorDefs[MT_COLOR_BOLD] && !search)
225 color = ColorDefs[MT_COLOR_BOLD];
229 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
230 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
231 color = ColorDefs[MT_COLOR_UNDERLINE];
233 color ^= A_UNDERLINE;
235 else if (a->attr & ANSI_REVERSE) {
238 else if (a->attr & ANSI_BLINK) {
241 else if (a->attr & ANSI_OFF) {
246 if (color != last_color) {
247 wattrset(main_w, color);
252 static void append_line (struct line_t *lineInfo, int n, int cnt)
256 lineInfo[n + 1].type = lineInfo[n].type;
257 lineInfo[n + 1].syntax[0].color = lineInfo[n].syntax[0].color;
258 lineInfo[n + 1].continuation = 1;
260 /* find the real start of the line */
261 for (m = n; m >= 0; m--)
262 if (lineInfo[m].continuation == 0)
265 (lineInfo[n + 1].syntax)[0].first = m;
266 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
267 cnt + (lineInfo[n].syntax)[0].last : cnt;
270 static void new_class_color (struct q_class_t *class, int *q_level)
272 class->index = (*q_level)++;
273 class->color = ColorQuote[class->index % ColorQuoteUsed];
277 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
278 int lindex, int *q_level)
280 struct q_class_t *q_list;
283 new_class->index = -1;
286 if (q_list->index >= lindex) {
288 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
291 q_list = q_list->down;
292 else if (q_list->next)
293 q_list = q_list->next;
295 while (!q_list->next) {
301 q_list = q_list->next;
305 new_class->index = lindex;
306 new_class->color = ColorQuote[lindex % ColorQuoteUsed];
310 static void cleanup_quote(struct q_class_t **QuoteList)
313 struct q_class_t *ptr = (*QuoteList)->next;
314 cleanup_quote(&(*QuoteList)->down);
315 p_delete(&(*QuoteList)->prefix);
321 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
322 const char *qptr, int length,
323 int *force_redraw, int *q_level)
325 struct q_class_t *q_list = *QuoteList;
326 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
328 int offset, tail_lng;
331 if (ColorQuoteUsed <= 1) {
332 /* not much point in classifying quotes... */
334 if (*QuoteList == NULL) {
335 class = p_new(struct q_class_t, 1);
336 class->color = ColorQuote[0];
342 /* Did I mention how much I like emulating Lisp in C? */
344 /* classify quoting prefix */
346 if (length <= q_list->length) {
347 /* case 1: check the top level nodes */
349 if (m_strncmp(qptr, q_list->prefix, length) == 0) {
350 if (length == q_list->length)
351 return q_list; /* same prefix: return the current class */
353 /* found shorter prefix */
355 /* add a node above q_list */
356 tmp = p_new(struct q_class_t, 1);
357 tmp->prefix = p_dupstr(qptr, length);
358 tmp->length = length;
360 /* replace q_list by tmp in the top level list */
362 tmp->next = q_list->next;
363 q_list->next->prev = tmp;
366 tmp->prev = q_list->prev;
367 q_list->prev->next = tmp;
370 /* make q_list a child of tmp */
374 /* q_list has no siblings for now */
378 /* update the root if necessary */
379 if (q_list == *QuoteList)
382 lindex = q_list->index;
384 /* tmp should be the return class too */
387 /* next class to test; if tmp is a shorter prefix for another
388 * node, that node can only be in the top level list, so don't
389 * go down after this point
393 /* found another branch for which tmp is a shorter prefix */
395 /* save the next sibling for later */
398 /* unlink q_list from the top level list */
400 q_list->next->prev = q_list->prev;
402 q_list->prev->next = q_list->next;
404 /* at this point, we have a tmp->down; link q_list to it */
406 /* sibling order is important here, q_list should be linked last */
414 lindex = q_list->index;
416 /* next class to test; as above, we shouldn't go down */
420 /* we found a shorter prefix, so certain quotes have changed classes */
424 /* shorter, but not a substring of the current class: try next */
425 q_list = q_list->next;
429 /* case 2: try subclassing the current top level node */
431 /* tmp != NULL means we already found a shorter prefix at case 1 */
433 && m_strncmp(qptr, q_list->prefix, q_list->length) == 0) {
434 /* ok, it's a subclass somewhere on this branch */
437 offset = q_list->length;
439 q_list = q_list->down;
440 tail_lng = length - offset;
441 tail_qptr = (char *) qptr + offset;
444 if (length <= q_list->length) {
445 if (m_strncmp(tail_qptr, (q_list->prefix) + offset, tail_lng)
447 /* same prefix: return the current class */
448 if (length == q_list->length)
451 /* found shorter common prefix */
453 /* add a node above q_list */
454 tmp = p_new(struct q_class_t, 1);
455 tmp->prefix = p_dupstr(qptr, length);
456 tmp->length = length;
458 /* replace q_list by tmp */
460 tmp->next = q_list->next;
461 q_list->next->prev = tmp;
464 tmp->prev = q_list->prev;
465 q_list->prev->next = tmp;
468 /* make q_list a child of tmp */
470 tmp->up = q_list->up;
472 if (tmp->up->down == q_list)
475 /* q_list has no siblings */
479 lindex = q_list->index;
481 /* tmp should be the return class too */
484 /* next class to test */
487 /* found another branch for which tmp is a shorter prefix */
489 /* save the next sibling for later */
492 /* unlink q_list from the top level list */
494 q_list->next->prev = q_list->prev;
496 q_list->prev->next = q_list->next;
498 /* at this point, we have a tmp->down; link q_list to it */
507 lindex = q_list->index;
509 /* next class to test */
513 /* we found a shorter prefix, so we need a redraw */
517 q_list = q_list->next;
521 /* longer than the current prefix: try subclassing it */
523 && m_strncmp(tail_qptr, (q_list->prefix) + offset,
524 q_list->length - offset) == 0) {
525 /* still a subclass: go down one level */
527 offset = q_list->length;
529 q_list = q_list->down;
530 tail_lng = length - offset;
531 tail_qptr = (char *) qptr + offset;
535 /* nope, try the next prefix */
536 q_list = q_list->next;
542 /* still not found so far: add it as a sibling to the current node */
544 tmp = p_new(struct q_class_t, 1);
545 tmp->prefix = p_dupstr(qptr, length);
546 tmp->length = length;
549 tmp->next = ptr->down;
550 ptr->down->prev = tmp;
555 new_class_color (tmp, q_level);
560 shift_class_colors (*QuoteList, tmp, lindex, q_level);
565 /* nope, try the next prefix */
566 q_list = q_list->next;
573 /* not found so far: add it as a top level class */
574 class = p_new(struct q_class_t, 1);
575 class->prefix = p_dupstr(qptr, length);
576 class->length = length;
577 new_class_color (class, q_level);
580 class->next = *QuoteList;
581 (*QuoteList)->prev = class;
587 shift_class_colors (*QuoteList, tmp, lindex, q_level);
592 static int check_attachment_marker (char *);
595 resolve_types (char *buf, char *rawbuf, struct line_t *lineInfo, int n, int last,
596 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
599 COLOR_LINE *color_line;
600 regmatch_t pmatch[1], smatch[1];
601 int found, offset, null_rx, i;
603 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
604 if (buf[0] == '\n') {
605 lineInfo[n].type = MT_COLOR_NORMAL;
607 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
608 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
609 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
611 lineInfo[n].type = MT_COLOR_HDEFAULT;
612 color_line = ColorHdrList;
614 if (REGEXEC (&color_line->rx, buf) == 0) {
615 lineInfo[n].type = MT_COLOR_HEADER;
616 lineInfo[n].syntax[0].color = color_line->pair;
619 color_line = color_line->next;
623 else if (m_strncmp("\033[0m", rawbuf, 4) == 0) /* a little hack... */
624 lineInfo[n].type = MT_COLOR_NORMAL;
625 else if (check_attachment_marker ((char *) rawbuf) == 0)
626 lineInfo[n].type = MT_COLOR_ATTACHMENT;
627 else if (m_strcmp("-- \n", buf) == 0
628 || m_strcmp("-- \r\n", buf) == 0) {
631 lineInfo[n].type = MT_COLOR_SIGNATURE;
632 while (i < last && check_sig(buf, lineInfo, i - 1) == 0 &&
633 (lineInfo[i].type == MT_COLOR_NORMAL ||
634 lineInfo[i].type == MT_COLOR_QUOTED ||
635 lineInfo[i].type == MT_COLOR_HEADER)) {
637 if (lineInfo[i].chunks) {
638 lineInfo[i].chunks = 0;
639 p_realloc(&(lineInfo[n].syntax), 1);
641 lineInfo[i++].type = MT_COLOR_SIGNATURE;
644 else if (check_sig(buf, lineInfo, n - 1) == 0)
645 lineInfo[n].type = MT_COLOR_SIGNATURE;
646 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
647 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
648 if (smatch[0].rm_so > 0) {
651 /* hack to avoid making an extra copy of buf */
652 c = buf[smatch[0].rm_so];
653 buf[smatch[0].rm_so] = 0;
655 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
656 if (q_classify && lineInfo[n].quote == NULL)
657 lineInfo[n].quote = classify_quote (QuoteList,
658 buf + pmatch[0].rm_so,
660 pmatch[0].rm_so, force_redraw,
662 lineInfo[n].type = MT_COLOR_QUOTED;
664 lineInfo[n].type = MT_COLOR_NORMAL;
667 buf[smatch[0].rm_so] = c;
669 lineInfo[n].type = MT_COLOR_NORMAL;
672 if (q_classify && lineInfo[n].quote == NULL)
673 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
674 pmatch[0].rm_eo - pmatch[0].rm_so,
675 force_redraw, q_level);
676 lineInfo[n].type = MT_COLOR_QUOTED;
679 lineInfo[n].type = MT_COLOR_NORMAL;
683 if (lineInfo[n].type == MT_COLOR_NORMAL ||
684 lineInfo[n].type == MT_COLOR_QUOTED) {
688 lineInfo[n].chunks = 0;
695 color_line = ColorBodyList;
697 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
698 (offset ? REG_NOTBOL : 0)) == 0) {
699 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
701 if (++(lineInfo[n].chunks) > 1)
702 p_realloc(&(lineInfo[n].syntax), lineInfo[n].chunks);
704 i = lineInfo[n].chunks - 1;
705 pmatch[0].rm_so += offset;
706 pmatch[0].rm_eo += offset;
708 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
709 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
710 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
711 (lineInfo[n].syntax)[i].color = color_line->pair;
712 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
713 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
718 null_rx = 1; /* empty regexp; don't add it, but keep looking */
721 color_line = color_line->next;
725 offset++; /* avoid degenerate cases */
727 offset = lineInfo[n].syntax[i].last;
728 } while (found || null_rx);
732 static int is_ansi(unsigned char *buf)
734 while (isdigit(*buf) || *buf == ';')
739 static int check_attachment_marker(char *p)
741 char *q = AttachmentMarker;
743 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
744 return (int)(*p - *q);
747 /* trim tail of buf so that it contains complete multibyte characters */
748 static int trim_incomplete_mbyte(unsigned char *buf, size_t len) {
752 p_clear(&mbstate, 1);
753 for (; len > 0; buf += k, len -= k) {
754 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
757 if (k == -1 || k == 0)
766 fill_buffer (FILE * f, off_t *last_pos, off_t offset, unsigned char *buf,
767 unsigned char *fmt, ssize_t blen, int *buf_ready)
770 static int b_read = 0;
772 if (*buf_ready == 0) {
774 if (offset != *last_pos)
775 fseeko (f, offset, 0);
776 if (fgets ((char *) buf, blen - 1, f) == NULL) {
780 *last_pos = ftello (f);
781 b_read = (int) (*last_pos - offset);
784 /* incomplete mbyte characters trigger a segfault in regex processing for
785 * certain versions of glibc. Trim them if necessary. */
786 if (b_read == blen - 2)
787 b_read -= trim_incomplete_mbyte(buf, b_read);
789 /* copy "buf" to "fmt", but without bold and underline controls */
792 if (*p == '\010' && (p > buf)) {
793 if (*(p + 1) == '_') /* underline */
795 else if (*(p + 1)) { /* bold or overstrike */
796 *(fmt - 1) = *(p + 1);
802 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
803 while (*p++ != 'm') /* skip ANSI sequence */
806 else if (*p == '\033' && *(p + 1) == ']'
807 && check_attachment_marker ((char *) p) == 0) {
808 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
819 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
820 int flags, ansi_attr * pa, int cnt,
821 int *pspace, int *pvch, int *pcol, int *pspecial)
823 int space = -1; /* index of the last space or TAB */
824 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
825 int ch, vch, k, last_special = -1, special = 0, t;
829 int wrap_cols = getmaxx(main_w);
831 if (!(flags & (M_SHOWFLAT)))
832 wrap_cols -= WrapMargin;
835 wrap_cols = getmaxx(main_w);
837 /* FIXME: this should come from lineInfo */
838 p_clear(&mbstate, 1);
840 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
841 /* Handle ANSI sequences */
842 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
843 is_ansi (buf + ch + 2)) {
844 while (isdigit(buf[ch]) || buf[ch] == ';')
848 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
849 check_attachment_marker ((char *) buf + ch) == 0) {
850 while (buf[ch++] != '\a')
855 /* is anything left to do? */
859 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
860 if (k == -2 || k == -1) {
861 if (col + 4 > wrap_cols)
865 wprintw(main_w, "\\%03o", buf[ch]);
872 /* Handle backspace */
879 while ((wc1 = 0, mbstate1 = mbstate,
881 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
882 &mbstate1), k1 - k > 0 && wc1 == '\b')
884 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
885 &mbstate1), k2 > 0 && iswprint(wc1))) {
887 special |= (wc == '_' && special & A_UNDERLINE)
888 ? A_UNDERLINE : A_BOLD;
890 else if (wc == '_' || wc1 == '_') {
891 special |= A_UNDERLINE;
892 wc = (wc1 == '_') ? wc : wc1;
894 /* special = 0; / * overstrike: nothing to do! */
904 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
905 special || last_special || pa->attr)) {
906 resolve_color (*lineInfo, n, vch, flags, special, pa);
907 last_special = special;
914 if (col + t > wrap_cols)
922 else if (wc == '\t') {
928 for (; col < t; col++)
933 else if (wc < 0x20 || wc == 0x7f) {
934 if (col + 2 > wrap_cols)
938 wprintw(main_w, "^%c", ('@' + wc) & 0x7f);
940 else if (wc < 0x100) {
941 if (col + 4 > wrap_cols)
945 wprintw(main_w, "\\%03o", wc);
947 if (col + 1 > wrap_cols)
951 waddch(main_w, CharsetReplacement);
963 * flags M_SHOWFLAT, show characters (used for displaying help)
964 * M_SHOWCOLOR, show characters in color
965 * otherwise don't show characters
966 * M_HIDE, don't show quoted text
967 * M_SEARCH, resolve search patterns
968 * M_TYPES, compute line's type
969 * M_PAGER_NSKIP, keeps leading whitespace
970 * M_PAGER_MARKER, eventually show markers
974 * 0 normal exit, line was not displayed
975 * >0 normal exit, line was displayed
978 display_line (FILE * f, off_t *last_pos, struct line_t **lineInfo, int n,
979 int *last, int *max, int flags, struct q_class_t **QuoteList,
980 int *q_level, int *force_redraw, regex_t * SearchRE)
982 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
983 unsigned char *buf_ptr = buf;
984 int ch, vch, col, cnt, b_read;
985 int buf_ready = 0, change_last = 0;
990 ansi_attr a = { 0, 0, 0, -1 };
991 regmatch_t pmatch[1];
999 p_realloc(lineInfo, *max += LINES);
1000 for (ch = *last; ch < *max; ch++) {
1001 p_clear(&(*lineInfo)[ch], 1);
1002 (*lineInfo)[ch].type = -1;
1003 (*lineInfo)[ch].search_cnt = -1;
1004 (*lineInfo)[ch].syntax = p_new(struct syntax_t, 1);
1005 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1010 /* only do color hiliting if we are viewing a message */
1011 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1012 if ((*lineInfo)[n].type == -1) {
1013 /* determine the line class */
1015 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1022 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1023 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1025 /* avoid race condition for continuation lines when scrolling up */
1027 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1029 (*lineInfo)[m].type = (*lineInfo)[n].type;
1032 /* this also prevents searching through the hidden lines */
1033 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1034 flags = 0; /* M_NOSHOW */
1037 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1038 * don't want to compute it every time M_TYPES is set, since this
1039 * would slow down the "bottom" function unacceptably. A compromise
1040 * solution is hence to call regexec() again, just to find out the
1041 * length of the quote prefix.
1043 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1044 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1047 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1053 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1054 (*lineInfo)[n].quote = classify_quote (QuoteList,
1055 (char *) fmt + pmatch[0].rm_so,
1056 pmatch[0].rm_eo - pmatch[0].rm_so,
1057 force_redraw, q_level);
1060 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1061 && (*lineInfo)[n].search_cnt == -1) {
1063 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1071 (*lineInfo)[n].search_cnt = 0;
1073 (SearchRE, (char *) fmt + offset, 1, pmatch,
1074 (offset ? REG_NOTBOL : 0)) == 0) {
1075 if (++((*lineInfo)[n].search_cnt) > 1)
1076 p_realloc(&(*lineInfo)[n].search, (*lineInfo)[n].search_cnt);
1078 (*lineInfo)[n].search = p_new(struct syntax_t, 1);
1079 pmatch[0].rm_so += offset;
1080 pmatch[0].rm_eo += offset;
1081 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1083 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1086 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1087 offset++; /* avoid degenerate cases */
1089 offset = pmatch[0].rm_eo;
1095 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1096 /* we've already scanned this line, so just exit */
1099 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1100 /* no need to try to display this line... */
1101 return 1; /* fake display */
1104 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1105 sizeof (buf), &buf_ready)) < 0) {
1111 /* now chose a good place to break the line */
1112 cnt = format_line(lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1114 buf_ptr = buf + cnt;
1116 /* move the break point only if smart_wrap is set */
1117 if (option (OPTWRAP)) {
1119 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1120 && buf[cnt] != '\r') {
1122 /* skip trailing blanks */
1123 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1125 /* a very long word with leading spaces causes infinite wrapping */
1126 if ((!ch) && (flags & M_PAGER_NSKIP))
1127 buf_ptr = buf + cnt;
1131 buf_ptr = buf + cnt; /* a very long word... */
1134 if (!(flags & M_PAGER_NSKIP))
1135 /* skip leading blanks on the next line too */
1136 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1140 if (*buf_ptr == '\r')
1142 if (*buf_ptr == '\n')
1145 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1146 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1147 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1149 /* if we don't need to display the line we are done */
1150 if (!(flags & M_SHOW))
1153 /* display the line */
1154 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1156 /* end the last color pattern (needed by S-Lang) */
1157 if (special || (col != getmaxx(main_w) && (flags & (M_SHOWCOLOR | M_SEARCH))))
1158 resolve_color(*lineInfo, n, vch, flags, 0, &a);
1161 * Fill the blank space at the end of the line with the prevailing color.
1162 * ncurses does an implicit wclrtoeol(main_w) when you do waddch(main_w,
1163 * '\n') so we have to make sure to reset the color *after* that
1165 if (flags & M_SHOWCOLOR) {
1166 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1167 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1168 def_color = ((*lineInfo)[m].syntax)[0].color;
1170 def_color = ColorDefs[(*lineInfo)[m].type];
1172 wattrset(main_w, def_color);
1173 wbkgdset(main_w, def_color | ' ');
1175 waddch(main_w, '\n');
1178 * reset the color back to normal. This *must* come after the
1179 * waddch(main_w, '\n'), otherwise the color for this line will not be
1180 * filled to the right margin.
1182 if (flags & M_SHOWCOLOR) {
1183 SETCOLOR(main_w, MT_COLOR_NORMAL);
1184 BKGDSET(main_w, MT_COLOR_NORMAL);
1187 /* build a return code */
1188 if (!(flags & M_SHOW))
1194 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1196 while (cur > 0 && nlines > 0) {
1198 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1205 /* This pager is actually not so simple as it once was. It now operates in
1206 two modes: one for viewing messages and the other for viewing help. These
1207 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1208 is there so that we can do operations on the current message without the
1209 need to pop back out to the main-menu. */
1211 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1213 static char searchbuf[STRING];
1214 char buffer[LONG_STRING];
1215 int maxLine, lastLine = 0;
1216 struct line_t *lineInfo;
1217 struct q_class_t *QuoteList = NULL;
1218 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1219 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1221 int redraw = REDRAW_FULL;
1223 off_t last_pos = 0, last_offset = 0;
1224 int old_smart_wrap, old_markers;
1227 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1228 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1230 int bodyoffset = 1; /* offset of first line of real text */
1231 int statusoffset = 0; /* offset for the status bar */
1232 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1234 MUTTMENU *pager_index = NULL; /* the Pager Index (PI) */
1235 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1236 int indicator = indexlen / 3; /* the indicator line of the PI */
1237 int old_PagerIndexLines; /* some people want to resize it
1238 * while inside the pager... */
1240 if (!(flags & M_SHOWCOLOR))
1241 flags |= M_SHOWFLAT;
1243 if ((fp = fopen (fname, "r")) == NULL) {
1244 mutt_perror (fname);
1248 if (stat (fname, &sb) != 0) {
1249 mutt_perror (fname);
1255 /* Initialize variables */
1257 if (IsHeader (extra) && !extra->hdr->read) {
1258 Context->msgnotreadyet = extra->hdr->msgno;
1259 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1262 lineInfo = p_new(struct line_t, maxLine = LINES);
1263 for (i = 0; i < maxLine; i++) {
1264 p_clear(&lineInfo[i], 1);
1265 lineInfo[i].type = -1;
1266 lineInfo[i].search_cnt = -1;
1267 lineInfo[i].syntax = p_new(struct syntax_t, 1);
1268 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1275 if (redraw & REDRAW_FULL) {
1276 SETCOLOR(main_w, MT_COLOR_NORMAL);
1279 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1280 indexlen = Context->vcount + 1;
1282 indexlen = PagerIndexLines;
1284 indicator = indexlen / 3;
1286 statusoffset = IsHeader (extra) ? indexlen : 0;
1287 bodyoffset = statusoffset + 1;
1288 bodylen = LINES - 2 - bodyoffset;
1290 SETCOLOR(main_w, MT_COLOR_SIDEBAR);
1291 mvwhline(main_w, LINES - 2, 0, ACS_HLINE, getmaxx(main_w));
1292 SETCOLOR(main_w, MT_COLOR_NORMAL);
1294 if (Resize != NULL) {
1295 if ((SearchCompiled = Resize->SearchCompiled)) {
1297 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1298 SearchFlag = M_SEARCH;
1299 SearchBack = Resize->SearchBack;
1301 lines = Resize->line;
1302 redraw |= REDRAW_SIGWINCH;
1307 if (IsHeader (extra) && PagerIndexLines) {
1308 if (pager_index == NULL) {
1309 /* only allocate the space if/when we need the index.
1310 Initialise the menu as per the main index */
1311 pager_index = mutt_new_menu ();
1312 pager_index->menu = MENU_MAIN;
1313 pager_index->make_entry = index_make_entry;
1314 pager_index->color = index_color;
1315 pager_index->max = Context->vcount;
1316 pager_index->current = extra->hdr->virtual;
1319 SETCOLOR(main_w, MT_COLOR_NORMAL);
1320 pager_index->offset = 1;
1321 pager_index->pagelen = indexlen - 1;
1323 /* some fudge to work out where abouts the indicator should go */
1324 if (pager_index->current - indicator < 0)
1325 pager_index->top = 0;
1326 else if (pager_index->max - pager_index->current < pager_index->pagelen - indicator)
1327 pager_index->top = pager_index->max - pager_index->pagelen;
1329 pager_index->top = pager_index->current - indicator;
1331 menu_redraw_index (pager_index);
1334 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1338 if (redraw & REDRAW_SIGWINCH) {
1341 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1342 has_types | SearchFlag, &QuoteList, &q_level,
1343 &force_redraw, &SearchRE) == 0) {
1344 if (!lineInfo[i].continuation && ++j == lines) {
1349 redraw |= REDRAW_SIDEBAR;
1353 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1355 wmove(main_w, bodyoffset, 0);
1356 curline = oldtopline = topline;
1360 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1361 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1363 (flags & M_DISPLAYFLAGS) | hideQuoted |
1364 SearchFlag, &QuoteList, &q_level, &force_redraw,
1368 wmove(main_w, lines + bodyoffset, 0);
1369 redraw |= REDRAW_SIDEBAR;
1371 last_offset = lineInfo[curline].offset;
1372 } while (force_redraw);
1374 SETCOLOR(main_w, MT_COLOR_TILDE);
1375 BKGDSET(main_w, MT_COLOR_TILDE);
1376 while (lines < bodylen) {
1378 if (option (OPTTILDE))
1379 waddch(main_w, '~');
1380 waddch(main_w, '\n');
1382 wmove(main_w, lines + bodyoffset, 0);
1384 /* We are going to update the pager status bar, so it isn't
1385 * necessary to reset to normal color now. */
1387 redraw |= REDRAW_STATUS; /* need to update the % seen */
1390 if (redraw & REDRAW_STATUS) {
1391 /* print out the pager status bar */
1392 SETCOLOR(main_w, MT_COLOR_STATUS);
1393 BKGDSET(main_w, MT_COLOR_STATUS);
1394 wmove(main_w, statusoffset, 0);
1396 if (IsHeader (extra)) {
1397 size_t l1 = (getmaxx(main_w) - 9) * MB_LEN_MAX;
1398 size_t l2 = sizeof (buffer);
1400 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1401 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1403 else if (IsMsgAttach (extra)) {
1404 size_t l1 = (getmaxx(main_w) - 9) * MB_LEN_MAX;
1405 size_t l2 = sizeof (buffer);
1407 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1408 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1410 wmove(main_w, statusoffset, 0);
1411 mutt_paddstr(main_w, getmaxx(main_w) - 10,
1412 IsHeader(extra) || IsMsgAttach(extra) ? buffer : banner);
1414 waddstr(main_w, " -- (");
1415 if (last_pos < sb.st_size - 1)
1416 wprintw(main_w, "%d%%)", (int) (100 * last_offset / sb.st_size));
1418 waddstr(main_w, topline == 0 ? "all)" : "end)");
1419 BKGDSET(main_w, MT_COLOR_NORMAL);
1420 SETCOLOR(main_w, MT_COLOR_NORMAL);
1423 if (redraw & REDRAW_SIDEBAR)
1426 if ((redraw & REDRAW_INDEX) && pager_index) {
1427 /* redraw the pager_index indicator, because the
1428 * flags for this message might have changed. */
1429 menu_redraw_current (pager_index);
1431 /* print out the pager_index status bar */
1432 menu_status_line (buffer, sizeof (buffer), pager_index, NONULL (Status));
1433 wmove(main_w, 0, 0);
1434 SETCOLOR(main_w, MT_COLOR_STATUS);
1435 BKGDSET(main_w, MT_COLOR_STATUS);
1436 mutt_paddstr(main_w, getmaxx(main_w), buffer);
1437 SETCOLOR(main_w, MT_COLOR_NORMAL);
1438 BKGDSET(main_w, MT_COLOR_NORMAL);
1445 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1446 && lineInfo[curline].offset < sb.st_size-1) {
1447 if (TopLine - topline > lines)
1456 ch = km_dokey (MENU_PAGER);
1458 mutt_clear_error ();
1469 /* Store current position. */
1471 for (i = 0; i <= topline; i++)
1472 if (!lineInfo[i].continuation)
1475 if (flags & M_PAGER_RETWINCH) {
1476 Resize = p_new(struct resize, 1);
1478 Resize->line = lines;
1479 Resize->SearchCompiled = SearchCompiled;
1480 Resize->SearchBack = SearchBack;
1483 rc = OP_REFORMAT_WINCH;
1485 for (i = 0; i < maxLine; i++) {
1486 lineInfo[i].offset = 0;
1487 lineInfo[i].type = -1;
1488 lineInfo[i].continuation = 0;
1489 lineInfo[i].chunks = 0;
1490 lineInfo[i].search_cnt = -1;
1491 lineInfo[i].quote = NULL;
1493 p_realloc(&lineInfo[i].syntax, 1);
1495 p_delete(&lineInfo[i].search);
1501 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1521 if (lineInfo[curline].offset < sb.st_size - 1) {
1522 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1524 else if (option (OPTPAGERSTOP)) {
1525 /* emulate "less -q" and don't go on to the next message. */
1526 mutt_error _("Bottom of message is shown.");
1528 /* end of the current message, so display the next message. */
1529 rc = OP_MAIN_NEXT_UNDELETED;
1537 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1540 mutt_error _("Top of message is shown.");
1544 if (lineInfo[curline].offset < sb.st_size - 1) {
1547 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1553 mutt_error _("Bottom of message is shown.");
1558 topline = upNLines (1, lineInfo, topline, hideQuoted);
1560 mutt_error _("Top of message is shown.");
1567 mutt_error _("Top of message is shown.");
1572 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1574 mutt_error _("Top of message is shown.");
1578 if (lineInfo[curline].offset < sb.st_size - 1) {
1579 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1581 else if (option (OPTPAGERSTOP)) {
1582 /* emulate "less -q" and don't go on to the next message. */
1583 mutt_error _("Bottom of message is shown.");
1585 /* end of the current message, so display the next message. */
1586 rc = OP_MAIN_NEXT_UNDELETED;
1591 case OP_SEARCH_NEXT:
1592 case OP_SEARCH_OPPOSITE:
1593 if (SearchCompiled) {
1595 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1596 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1597 /* searching forward */
1598 for (i = topline + 1; i < lastLine; i++) {
1599 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1600 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1607 mutt_error _("Not found.");
1610 /* searching backward */
1611 for (i = topline - 1; i >= 0; i--) {
1612 if ((!hideQuoted || (has_types &&
1613 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1614 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1621 mutt_error _("Not found.");
1624 if (lineInfo[topline].search_cnt > 0)
1625 SearchFlag = M_SEARCH;
1629 /* no previous search pattern, so fall through to search */
1632 case OP_SEARCH_REVERSE:
1633 m_strcpy(buffer, sizeof(buffer), searchbuf);
1634 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1635 _("Search: ")), buffer, sizeof (buffer),
1639 if (!m_strcmp (buffer, searchbuf)) {
1640 if (SearchCompiled) {
1641 /* do an implicit search-next */
1642 if (ch == OP_SEARCH)
1643 ch = OP_SEARCH_NEXT;
1645 ch = OP_SEARCH_OPPOSITE;
1654 m_strcpy(searchbuf, sizeof(searchbuf), buffer);
1656 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1657 if (ch == OP_SEARCH)
1659 else if (ch == OP_SEARCH_REVERSE)
1662 if (SearchCompiled) {
1663 regfree (&SearchRE);
1664 for (i = 0; i < lastLine; i++) {
1665 p_delete(&lineInfo[i].search);
1666 lineInfo[i].search_cnt = -1;
1671 REGCOMP (&SearchRE, searchbuf,
1672 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1673 regerror (err, &SearchRE, buffer, sizeof (buffer));
1674 mutt_error ("%s", buffer);
1675 regfree (&SearchRE);
1676 for (i = 0; i < maxLine; i++) {
1678 p_delete(&lineInfo[i].search);
1679 lineInfo[i].search_cnt = -1;
1686 /* update the search pointers */
1688 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1689 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1690 &QuoteList, &q_level,
1691 &force_redraw, &SearchRE) == 0) {
1693 redraw |= REDRAW_SIDEBAR;
1697 /* searching forward */
1698 for (i = topline; i < lastLine; i++) {
1699 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1700 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1708 /* searching backward */
1709 for (i = topline; i >= 0; i--) {
1710 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1711 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1719 if (lineInfo[topline].search_cnt == 0) {
1721 mutt_error _("Not found.");
1724 SearchFlag = M_SEARCH;
1726 redraw = REDRAW_BODY;
1729 case OP_SEARCH_TOGGLE:
1730 if (SearchCompiled) {
1731 SearchFlag ^= M_SEARCH;
1732 redraw = REDRAW_BODY;
1737 /* don't let the user enter the help-menu from the help screen! */
1740 mutt_help (MENU_PAGER);
1741 redraw = REDRAW_FULL;
1745 mutt_error _("Help is currently being shown.");
1748 case OP_PAGER_HIDE_QUOTED:
1750 hideQuoted ^= M_HIDE;
1751 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1752 topline = upNLines (1, lineInfo, topline, hideQuoted);
1754 redraw = REDRAW_BODY;
1758 case OP_PAGER_SKIP_QUOTED:
1761 int new_topline = topline;
1763 while ((new_topline < lastLine ||
1764 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1765 new_topline, &lastLine,
1766 &maxLine, M_TYPES, &QuoteList,
1767 &q_level, &force_redraw,
1769 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
1770 redraw |= REDRAW_SIDEBAR;
1775 mutt_error _("No more quoted text.");
1780 while ((new_topline < lastLine ||
1781 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1782 new_topline, &lastLine,
1783 &maxLine, M_TYPES, &QuoteList,
1784 &q_level, &force_redraw,
1786 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
1788 redraw |= REDRAW_SIDEBAR;
1792 mutt_error _("No more unquoted text after quoted text.");
1796 topline = new_topline;
1800 case OP_PAGER_BOTTOM: /* move to the end of the file */
1801 if (lineInfo[curline].offset < sb.st_size - 1) {
1803 /* make sure the types are defined to the end of file */
1804 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1805 &maxLine, has_types,
1806 &QuoteList, &q_level, &force_redraw,
1809 redraw |= REDRAW_SIDEBAR;
1811 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
1814 mutt_error _("Bottom of message is shown.");
1818 clearok(main_w, TRUE);
1819 redraw = REDRAW_FULL;
1823 km_error_key (MENU_PAGER);
1826 /* --------------------------------------------------------------------
1827 * The following are operations on the current message rather than
1828 * adjusting the view of the message.
1831 case OP_BOUNCE_MESSAGE:
1832 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
1834 if (IsMsgAttach (extra))
1835 mutt_attach_bounce (extra->fp, extra->hdr,
1836 extra->idx, extra->idxlen, extra->bdy);
1838 ci_bounce_message (extra->hdr, &redraw);
1842 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
1844 if (IsMsgAttach (extra))
1845 mutt_attach_resend (extra->fp, extra->hdr,
1846 extra->idx, extra->idxlen, extra->bdy);
1848 mutt_resend_message (NULL, extra->ctx, extra->hdr);
1849 redraw = REDRAW_FULL;
1852 case OP_CHECK_TRADITIONAL:
1853 CHECK_MODE (IsHeader (extra));
1854 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
1856 rc = OP_CHECK_TRADITIONAL;
1860 case OP_CREATE_ALIAS:
1861 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
1862 if (IsMsgAttach (extra))
1863 mutt_create_alias (extra->bdy->hdr->env, NULL);
1865 mutt_create_alias (extra->hdr->env, NULL);
1866 MAYBE_REDRAW (redraw);
1869 case OP_PURGE_MESSAGE:
1871 CHECK_MODE (IsHeader (extra));
1874 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
1876 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
1877 mutt_set_flag (Context, extra->hdr, M_PURGED,
1878 ch != OP_PURGE_MESSAGE ? 0 : 1);
1879 if (option (OPTDELETEUNTAG))
1880 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
1881 redraw = REDRAW_STATUS | REDRAW_INDEX;
1882 if (option (OPTRESOLVE)) {
1884 rc = OP_MAIN_NEXT_UNDELETED;
1888 case OP_DELETE_THREAD:
1889 case OP_DELETE_SUBTHREAD:
1890 CHECK_MODE (IsHeader (extra));
1893 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
1895 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
1896 ch == OP_DELETE_THREAD ? 0 : 1);
1899 if (option (OPTDELETEUNTAG))
1900 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
1901 ch == OP_DELETE_THREAD ? 0 : 1);
1902 if (option (OPTRESOLVE)) {
1903 rc = OP_MAIN_NEXT_UNDELETED;
1907 if (!option (OPTRESOLVE) && PagerIndexLines)
1908 redraw = REDRAW_FULL;
1910 redraw = REDRAW_STATUS | REDRAW_INDEX;
1914 case OP_DISPLAY_ADDRESS:
1915 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
1916 if (IsMsgAttach (extra))
1917 mutt_display_address (extra->bdy->hdr->env);
1919 mutt_display_address (extra->hdr->env);
1922 case OP_ENTER_COMMAND:
1923 old_smart_wrap = option (OPTWRAP);
1924 old_markers = option (OPTMARKERS);
1925 old_PagerIndexLines = PagerIndexLines;
1927 CurrentMenu = MENU_PAGER;
1928 mutt_enter_command ();
1930 if (option (OPTNEEDRESORT)) {
1931 unset_option (OPTNEEDRESORT);
1932 CHECK_MODE (IsHeader (extra));
1933 set_option (OPTNEEDRESORT);
1936 if (old_PagerIndexLines != PagerIndexLines) {
1938 mutt_menuDestroy (&pager_index);
1942 if (option (OPTWRAP) != old_smart_wrap ||
1943 option (OPTMARKERS) != old_markers) {
1944 if (flags & M_PAGER_RETWINCH) {
1946 rc = OP_REFORMAT_WINCH;
1950 /* count the real lines above */
1952 for (i = 0; i <= topline; i++) {
1953 if (!lineInfo[i].continuation)
1957 /* we need to restart the whole thing */
1958 for (i = 0; i < maxLine; i++) {
1959 lineInfo[i].offset = 0;
1960 lineInfo[i].type = -1;
1961 lineInfo[i].continuation = 0;
1962 lineInfo[i].chunks = 0;
1963 lineInfo[i].search_cnt = -1;
1964 lineInfo[i].quote = NULL;
1966 p_realloc(&(lineInfo[i].syntax), 1);
1968 p_delete(&lineInfo[i].search);
1971 if (SearchCompiled) {
1972 regfree (&SearchRE);
1977 /* try to keep the old position */
1980 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
1981 &lastLine, &maxLine,
1982 (has_types ? M_TYPES : 0),
1983 &QuoteList, &q_level, &force_redraw,
1985 redraw |= REDRAW_SIDEBAR;
1986 if (!lineInfo[topline].continuation)
1995 if (option (OPTFORCEREDRAWPAGER))
1996 redraw = REDRAW_FULL;
1997 unset_option (OPTFORCEREDRAWINDEX);
1998 unset_option (OPTFORCEREDRAWPAGER);
2001 case OP_FLAG_MESSAGE:
2002 CHECK_MODE (IsHeader (extra));
2005 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2007 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2008 redraw = REDRAW_STATUS | REDRAW_INDEX;
2009 if (option (OPTRESOLVE)) {
2011 rc = OP_MAIN_NEXT_UNDELETED;
2016 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2017 if (IsAttach (extra))
2018 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2020 mutt_pipe_message (extra->hdr);
2021 MAYBE_REDRAW (redraw);
2025 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2026 if (IsAttach (extra))
2027 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2029 mutt_print_message (extra->hdr);
2033 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2035 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2036 redraw = REDRAW_FULL;
2040 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2042 if (IsMsgAttach (extra))
2043 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2044 extra->idxlen, extra->bdy, SENDREPLY);
2046 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2047 redraw = REDRAW_FULL;
2050 case OP_RECALL_MESSAGE:
2051 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2053 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2054 redraw = REDRAW_FULL;
2057 case OP_GROUP_REPLY:
2058 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2060 if (IsMsgAttach (extra))
2061 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2062 extra->idxlen, extra->bdy,
2063 SENDREPLY | SENDGROUPREPLY);
2065 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2067 redraw = REDRAW_FULL;
2071 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2073 if (IsMsgAttach (extra))
2074 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2075 extra->idxlen, extra->bdy,
2076 SENDREPLY | SENDLISTREPLY);
2078 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2080 redraw = REDRAW_FULL;
2083 case OP_FORWARD_MESSAGE:
2084 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2086 if (IsMsgAttach (extra))
2087 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2088 extra->idxlen, extra->bdy, 0);
2090 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2091 redraw = REDRAW_FULL;
2094 case OP_DECRYPT_SAVE:
2096 if (IsAttach (extra)) {
2097 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2102 case OP_COPY_MESSAGE:
2103 case OP_DECODE_SAVE:
2104 case OP_DECODE_COPY:
2105 case OP_DECRYPT_COPY:
2106 CHECK_MODE (IsHeader (extra));
2107 if (mutt_save_message (extra->hdr,
2108 (ch == OP_DECRYPT_SAVE) ||
2109 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2110 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2111 (ch == OP_DECRYPT_SAVE)
2112 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2113 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2114 || ch == OP_DECRYPT_SAVE)) {
2115 if (option (OPTRESOLVE)) {
2117 rc = OP_MAIN_NEXT_UNDELETED;
2120 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2122 MAYBE_REDRAW (redraw);
2125 case OP_SHELL_ESCAPE:
2126 mutt_shell_escape ();
2127 MAYBE_REDRAW (redraw);
2131 CHECK_MODE (IsHeader (extra));
2132 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2134 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2135 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2136 ? NULL : Context->last_tag);
2138 redraw = REDRAW_STATUS | REDRAW_INDEX;
2139 if (option (OPTRESOLVE)) {
2146 CHECK_MODE (IsHeader (extra));
2149 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2151 if (extra->hdr->read || extra->hdr->old)
2152 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2154 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2156 Context->msgnotreadyet = -1;
2157 redraw = REDRAW_STATUS | REDRAW_INDEX;
2158 if (option (OPTRESOLVE)) {
2160 rc = OP_MAIN_NEXT_UNDELETED;
2165 CHECK_MODE (IsHeader (extra));
2168 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2170 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2171 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2172 redraw = REDRAW_STATUS | REDRAW_INDEX;
2173 if (option (OPTRESOLVE)) {
2179 case OP_UNDELETE_THREAD:
2180 case OP_UNDELETE_SUBTHREAD:
2181 CHECK_MODE (IsHeader (extra));
2184 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2186 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2187 ch == OP_UNDELETE_THREAD ? 0 : 1)
2188 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2189 ch == OP_UNDELETE_THREAD ? 0 : 1);
2192 if (option (OPTRESOLVE)) {
2193 rc = (ch == OP_DELETE_THREAD) ?
2194 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2198 if (!option (OPTRESOLVE) && PagerIndexLines)
2199 redraw = REDRAW_FULL;
2201 redraw = REDRAW_STATUS | REDRAW_INDEX;
2206 if (option (OPTFORCEBUFFYCHECK))
2209 redraw |= REDRAW_SIDEBAR;
2212 case OP_VIEW_ATTACHMENTS:
2213 if (flags & M_PAGER_ATTACHMENT) {
2215 rc = OP_ATTACH_COLLAPSE;
2218 CHECK_MODE (IsHeader (extra));
2219 mutt_view_attachments (extra->hdr);
2220 if (extra->hdr->attach_del)
2221 Context->changed = 1;
2222 redraw = REDRAW_FULL;
2225 case OP_EXTRACT_KEYS:
2226 CHECK_MODE (IsHeader (extra));
2227 crypt_extract_keys_from_messages (extra->hdr);
2228 redraw = REDRAW_FULL;
2231 case OP_SIDEBAR_SCROLL_UP:
2232 case OP_SIDEBAR_SCROLL_DOWN:
2233 case OP_SIDEBAR_NEXT:
2234 case OP_SIDEBAR_NEXT_NEW:
2235 case OP_SIDEBAR_PREV:
2236 case OP_SIDEBAR_PREV_NEW:
2237 sidebar_scroll (ch);
2246 if (IsHeader (extra)) {
2247 Context->msgnotreadyet = -1;
2252 OldHdr = extra->hdr;
2256 cleanup_quote(&QuoteList);
2258 for (i = 0; i < maxLine; i++) {
2259 p_delete(&lineInfo[i].syntax);
2261 p_delete(&lineInfo[i].search);
2263 if (SearchCompiled) {
2264 regfree (&SearchRE);
2267 p_delete(&lineInfo);
2269 mutt_menuDestroy (&pager_index);
2270 return rc != -1 ? rc : 0;
2274 #undef CHECK_READONLY