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>
15 #include <lib-crypt/crypt.h>
17 #include <lib-ui/curses.h>
18 #include <lib-ui/enter.h>
19 #include <lib-ui/menu.h>
20 #include <lib-ui/sidebar.h>
21 #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 (USE_SLANG_CURSES) || 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) {
221 if ((a->attr & ANSI_COLOR)) {
223 a->pair = mutt_alloc_color (a->fg, a->bg);
225 if (a->attr & ANSI_BOLD)
230 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
231 if (ColorDefs[MT_COLOR_BOLD] && !search)
232 color = ColorDefs[MT_COLOR_BOLD];
236 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
237 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
238 color = ColorDefs[MT_COLOR_UNDERLINE];
240 color ^= A_UNDERLINE;
242 else if (a->attr & ANSI_REVERSE) {
245 else if (a->attr & ANSI_BLINK) {
248 else if (a->attr & ANSI_OFF) {
253 if (color != last_color) {
259 static void append_line (struct line_t *lineInfo, int n, int cnt)
263 lineInfo[n + 1].type = lineInfo[n].type;
264 lineInfo[n + 1].syntax[0].color = lineInfo[n].syntax[0].color;
265 lineInfo[n + 1].continuation = 1;
267 /* find the real start of the line */
268 for (m = n; m >= 0; m--)
269 if (lineInfo[m].continuation == 0)
272 (lineInfo[n + 1].syntax)[0].first = m;
273 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
274 cnt + (lineInfo[n].syntax)[0].last : cnt;
277 static void new_class_color (struct q_class_t *class, int *q_level)
279 class->index = (*q_level)++;
280 class->color = ColorQuote[class->index % ColorQuoteUsed];
284 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
285 int lindex, int *q_level)
287 struct q_class_t *q_list;
290 new_class->index = -1;
293 if (q_list->index >= lindex) {
295 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
298 q_list = q_list->down;
299 else if (q_list->next)
300 q_list = q_list->next;
302 while (!q_list->next) {
308 q_list = q_list->next;
312 new_class->index = lindex;
313 new_class->color = ColorQuote[lindex % ColorQuoteUsed];
317 static void cleanup_quote (struct q_class_t **QuoteList)
319 struct q_class_t *ptr;
322 if ((*QuoteList)->down)
323 cleanup_quote (&((*QuoteList)->down));
324 ptr = (*QuoteList)->next;
325 if ((*QuoteList)->prefix)
326 p_delete(&(*QuoteList)->prefix);
334 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
335 const char *qptr, int length,
336 int *force_redraw, int *q_level)
338 struct q_class_t *q_list = *QuoteList;
339 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
341 int offset, tail_lng;
344 if (ColorQuoteUsed <= 1) {
345 /* not much point in classifying quotes... */
347 if (*QuoteList == NULL) {
348 class = p_new(struct q_class_t, 1);
349 class->color = ColorQuote[0];
355 /* Did I mention how much I like emulating Lisp in C? */
357 /* classify quoting prefix */
359 if (length <= q_list->length) {
360 /* case 1: check the top level nodes */
362 if (m_strncmp(qptr, q_list->prefix, length) == 0) {
363 if (length == q_list->length)
364 return q_list; /* same prefix: return the current class */
366 /* found shorter prefix */
368 /* add a node above q_list */
369 tmp = p_new(struct q_class_t, 1);
370 tmp->prefix = p_dupstr(qptr, length);
371 tmp->length = length;
373 /* replace q_list by tmp in the top level list */
375 tmp->next = q_list->next;
376 q_list->next->prev = tmp;
379 tmp->prev = q_list->prev;
380 q_list->prev->next = tmp;
383 /* make q_list a child of tmp */
387 /* q_list has no siblings for now */
391 /* update the root if necessary */
392 if (q_list == *QuoteList)
395 lindex = q_list->index;
397 /* tmp should be the return class too */
400 /* next class to test; if tmp is a shorter prefix for another
401 * node, that node can only be in the top level list, so don't
402 * go down after this point
406 /* found another branch for which tmp is a shorter prefix */
408 /* save the next sibling for later */
411 /* unlink q_list from the top level list */
413 q_list->next->prev = q_list->prev;
415 q_list->prev->next = q_list->next;
417 /* at this point, we have a tmp->down; link q_list to it */
419 /* sibling order is important here, q_list should be linked last */
427 lindex = q_list->index;
429 /* next class to test; as above, we shouldn't go down */
433 /* we found a shorter prefix, so certain quotes have changed classes */
437 /* shorter, but not a substring of the current class: try next */
438 q_list = q_list->next;
442 /* case 2: try subclassing the current top level node */
444 /* tmp != NULL means we already found a shorter prefix at case 1 */
446 && m_strncmp(qptr, q_list->prefix, q_list->length) == 0) {
447 /* ok, it's a subclass somewhere on this branch */
450 offset = q_list->length;
452 q_list = q_list->down;
453 tail_lng = length - offset;
454 tail_qptr = (char *) qptr + offset;
457 if (length <= q_list->length) {
458 if (m_strncmp(tail_qptr, (q_list->prefix) + offset, tail_lng)
460 /* same prefix: return the current class */
461 if (length == q_list->length)
464 /* found shorter common prefix */
466 /* add a node above q_list */
467 tmp = p_new(struct q_class_t, 1);
468 tmp->prefix = p_dupstr(qptr, length);
469 tmp->length = length;
471 /* replace q_list by tmp */
473 tmp->next = q_list->next;
474 q_list->next->prev = tmp;
477 tmp->prev = q_list->prev;
478 q_list->prev->next = tmp;
481 /* make q_list a child of tmp */
483 tmp->up = q_list->up;
485 if (tmp->up->down == q_list)
488 /* q_list has no siblings */
492 lindex = q_list->index;
494 /* tmp should be the return class too */
497 /* next class to test */
500 /* found another branch for which tmp is a shorter prefix */
502 /* save the next sibling for later */
505 /* unlink q_list from the top level list */
507 q_list->next->prev = q_list->prev;
509 q_list->prev->next = q_list->next;
511 /* at this point, we have a tmp->down; link q_list to it */
520 lindex = q_list->index;
522 /* next class to test */
526 /* we found a shorter prefix, so we need a redraw */
530 q_list = q_list->next;
534 /* longer than the current prefix: try subclassing it */
536 && m_strncmp(tail_qptr, (q_list->prefix) + offset,
537 q_list->length - offset) == 0) {
538 /* still a subclass: go down one level */
540 offset = q_list->length;
542 q_list = q_list->down;
543 tail_lng = length - offset;
544 tail_qptr = (char *) qptr + offset;
548 /* nope, try the next prefix */
549 q_list = q_list->next;
555 /* still not found so far: add it as a sibling to the current node */
557 tmp = p_new(struct q_class_t, 1);
558 tmp->prefix = p_dupstr(qptr, length);
559 tmp->length = length;
562 tmp->next = ptr->down;
563 ptr->down->prev = tmp;
568 new_class_color (tmp, q_level);
573 shift_class_colors (*QuoteList, tmp, lindex, q_level);
578 /* nope, try the next prefix */
579 q_list = q_list->next;
586 /* not found so far: add it as a top level class */
587 class = p_new(struct q_class_t, 1);
588 class->prefix = p_dupstr(qptr, length);
589 class->length = length;
590 new_class_color (class, q_level);
593 class->next = *QuoteList;
594 (*QuoteList)->prev = class;
600 shift_class_colors (*QuoteList, tmp, lindex, q_level);
605 static int brailleLine = -1;
606 static int brailleCol = -1;
608 static int check_attachment_marker (char *);
611 resolve_types (char *buf, char *rawbuf, struct line_t *lineInfo, int n, int last,
612 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
615 COLOR_LINE *color_line;
616 regmatch_t pmatch[1], smatch[1];
617 int found, offset, null_rx, i;
619 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
620 if (buf[0] == '\n') {
621 lineInfo[n].type = MT_COLOR_NORMAL;
622 getyx(stdscr, brailleLine, brailleCol);
624 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
625 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
626 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
628 lineInfo[n].type = MT_COLOR_HDEFAULT;
629 color_line = ColorHdrList;
631 if (REGEXEC (&color_line->rx, buf) == 0) {
632 lineInfo[n].type = MT_COLOR_HEADER;
633 lineInfo[n].syntax[0].color = color_line->pair;
636 color_line = color_line->next;
640 else if (m_strncmp("\033[0m", rawbuf, 4) == 0) /* a little hack... */
641 lineInfo[n].type = MT_COLOR_NORMAL;
642 else if (check_attachment_marker ((char *) rawbuf) == 0)
643 lineInfo[n].type = MT_COLOR_ATTACHMENT;
644 else if (m_strcmp("-- \n", buf) == 0
645 || m_strcmp("-- \r\n", buf) == 0) {
648 lineInfo[n].type = MT_COLOR_SIGNATURE;
649 while (i < last && check_sig(buf, lineInfo, i - 1) == 0 &&
650 (lineInfo[i].type == MT_COLOR_NORMAL ||
651 lineInfo[i].type == MT_COLOR_QUOTED ||
652 lineInfo[i].type == MT_COLOR_HEADER)) {
654 if (lineInfo[i].chunks) {
655 lineInfo[i].chunks = 0;
656 p_realloc(&(lineInfo[n].syntax), 1);
658 lineInfo[i++].type = MT_COLOR_SIGNATURE;
661 else if (check_sig(buf, lineInfo, n - 1) == 0)
662 lineInfo[n].type = MT_COLOR_SIGNATURE;
663 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
664 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
665 if (smatch[0].rm_so > 0) {
668 /* hack to avoid making an extra copy of buf */
669 c = buf[smatch[0].rm_so];
670 buf[smatch[0].rm_so] = 0;
672 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
673 if (q_classify && lineInfo[n].quote == NULL)
674 lineInfo[n].quote = classify_quote (QuoteList,
675 buf + pmatch[0].rm_so,
677 pmatch[0].rm_so, force_redraw,
679 lineInfo[n].type = MT_COLOR_QUOTED;
681 lineInfo[n].type = MT_COLOR_NORMAL;
684 buf[smatch[0].rm_so] = c;
686 lineInfo[n].type = MT_COLOR_NORMAL;
689 if (q_classify && lineInfo[n].quote == NULL)
690 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
691 pmatch[0].rm_eo - pmatch[0].rm_so,
692 force_redraw, q_level);
693 lineInfo[n].type = MT_COLOR_QUOTED;
696 lineInfo[n].type = MT_COLOR_NORMAL;
700 if (lineInfo[n].type == MT_COLOR_NORMAL ||
701 lineInfo[n].type == MT_COLOR_QUOTED) {
705 lineInfo[n].chunks = 0;
712 color_line = ColorBodyList;
714 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
715 (offset ? REG_NOTBOL : 0)) == 0) {
716 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
718 if (++(lineInfo[n].chunks) > 1)
719 p_realloc(&(lineInfo[n].syntax), lineInfo[n].chunks);
721 i = lineInfo[n].chunks - 1;
722 pmatch[0].rm_so += offset;
723 pmatch[0].rm_eo += offset;
725 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
726 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
727 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
728 (lineInfo[n].syntax)[i].color = color_line->pair;
729 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
730 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
735 null_rx = 1; /* empty regexp; don't add it, but keep looking */
738 color_line = color_line->next;
742 offset++; /* avoid degenerate cases */
744 offset = lineInfo[n].syntax[i].last;
745 } while (found || null_rx);
749 static int is_ansi(unsigned char *buf)
751 while (isdigit(*buf) || *buf == ';')
753 return (*buf == 'm');
756 static int check_attachment_marker(char *p)
758 char *q = AttachmentMarker;
760 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
761 return (int)(*p - *q);
764 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
768 while (isdigit (buf[x]) || buf[x] == ';')
771 /* Character Attributes */
772 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
776 mutt_free_color (a->fg, a->bg);
784 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
785 a->attr |= ANSI_BOLD;
788 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
789 a->attr |= ANSI_UNDERLINE;
792 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
793 a->attr |= ANSI_BLINK;
796 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
797 a->attr |= ANSI_REVERSE;
800 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
805 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
808 mutt_free_color (a->fg, a->bg);
811 a->attr |= ANSI_COLOR;
812 if (buf[pos + 1] != '9')
813 a->fg = buf[pos + 1] - '0';
816 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
819 mutt_free_color (a->fg, a->bg);
822 a->attr |= ANSI_COLOR;
823 if (buf[pos + 1] != '9')
824 a->bg = buf[pos + 1] - '0';
827 while (pos < x && buf[pos] != ';')
837 /* trim tail of buf so that it contains complete multibyte characters */
838 static int trim_incomplete_mbyte(unsigned char *buf, size_t len) {
842 p_clear(&mbstate, 1);
843 for (; len > 0; buf += k, len -= k) {
844 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
847 if (k == -1 || k == 0)
856 fill_buffer (FILE * f, off_t *last_pos, off_t offset, unsigned char *buf,
857 unsigned char *fmt, ssize_t blen, int *buf_ready)
860 static int b_read = 0;
862 if (*buf_ready == 0) {
864 if (offset != *last_pos)
865 fseeko (f, offset, 0);
866 if (fgets ((char *) buf, blen - 1, f) == NULL) {
870 *last_pos = ftello (f);
871 b_read = (int) (*last_pos - offset);
874 /* incomplete mbyte characters trigger a segfault in regex processing for
875 * certain versions of glibc. Trim them if necessary. */
876 if (b_read == blen - 2)
877 b_read -= trim_incomplete_mbyte(buf, b_read);
879 /* copy "buf" to "fmt", but without bold and underline controls */
882 if (*p == '\010' && (p > buf)) {
883 if (*(p + 1) == '_') /* underline */
885 else if (*(p + 1)) { /* bold or overstrike */
886 *(fmt - 1) = *(p + 1);
892 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
893 while (*p++ != 'm') /* skip ANSI sequence */
896 else if (*p == '\033' && *(p + 1) == ']'
897 && check_attachment_marker ((char *) p) == 0) {
898 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
910 #include <nntp/nntp.h>
913 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
914 int flags, ansi_attr * pa, int cnt,
915 int *pspace, int *pvch, int *pcol, int *pspecial)
917 int space = -1; /* index of the last space or TAB */
918 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
919 int ch, vch, k, last_special = -1, special = 0, t;
923 int wrap_cols = COLS;
925 if (!(flags & (M_SHOWFLAT)))
926 wrap_cols -= WrapMargin;
932 /* FIXME: this should come from lineInfo */
933 p_clear(&mbstate, 1);
935 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
936 /* Handle ANSI sequences */
937 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
938 is_ansi (buf + ch + 2))
939 ch = grok_ansi (buf, ch + 2, pa) + 1;
941 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
942 check_attachment_marker ((char *) buf + ch) == 0) {
943 while (buf[ch++] != '\a')
948 /* is anything left to do? */
952 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
953 if (k == -2 || k == -1) {
954 if (col + 4 > wrap_cols)
958 printw ("\\%03o", buf[ch]);
965 /* Handle backspace */
972 while ((wc1 = 0, mbstate1 = mbstate,
974 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
975 &mbstate1), k1 - k > 0 && wc1 == '\b')
977 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
978 &mbstate1), k2 > 0 && iswprint(wc1))) {
980 special |= (wc == '_' && special & A_UNDERLINE)
981 ? A_UNDERLINE : A_BOLD;
983 else if (wc == '_' || wc1 == '_') {
984 special |= A_UNDERLINE;
985 wc = (wc1 == '_') ? wc : wc1;
987 /* special = 0; / * overstrike: nothing to do! */
997 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
998 special || last_special || pa->attr)) {
999 resolve_color (*lineInfo, n, vch, flags, special, pa);
1000 last_special = special;
1007 if (col + t > wrap_cols)
1013 else if (wc == '\n')
1015 else if (wc == '\t') {
1021 for (; col < t; col++)
1026 else if (wc < 0x20 || wc == 0x7f) {
1027 if (col + 2 > wrap_cols)
1031 printw ("^%c", ('@' + wc) & 0x7f);
1033 else if (wc < 0x100) {
1034 if (col + 4 > wrap_cols)
1038 printw ("\\%03o", wc);
1040 if (col + 1 > wrap_cols)
1044 addch (CharsetReplacement);
1050 *pspecial = special;
1056 * flags M_SHOWFLAT, show characters (used for displaying help)
1057 * M_SHOWCOLOR, show characters in color
1058 * otherwise don't show characters
1059 * M_HIDE, don't show quoted text
1060 * M_SEARCH, resolve search patterns
1061 * M_TYPES, compute line's type
1062 * M_PAGER_NSKIP, keeps leading whitespace
1063 * M_PAGER_MARKER, eventually show markers
1066 * -1 EOF was reached
1067 * 0 normal exit, line was not displayed
1068 * >0 normal exit, line was displayed
1071 display_line (FILE * f, off_t *last_pos, struct line_t **lineInfo, int n,
1072 int *last, int *max, int flags, struct q_class_t **QuoteList,
1073 int *q_level, int *force_redraw, regex_t * SearchRE)
1075 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1076 unsigned char *buf_ptr = buf;
1077 int ch, vch, col, cnt, b_read;
1078 int buf_ready = 0, change_last = 0;
1083 ansi_attr a = { 0, 0, 0, -1 };
1084 regmatch_t pmatch[1];
1091 if (*last == *max) {
1092 p_realloc(lineInfo, *max += LINES);
1093 for (ch = *last; ch < *max; ch++) {
1094 p_clear(&(*lineInfo)[ch], 1);
1095 (*lineInfo)[ch].type = -1;
1096 (*lineInfo)[ch].search_cnt = -1;
1097 (*lineInfo)[ch].syntax = p_new(struct syntax_t, 1);
1098 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1103 /* only do color hiliting if we are viewing a message */
1104 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1105 if ((*lineInfo)[n].type == -1) {
1106 /* determine the line class */
1108 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1115 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1116 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1118 /* avoid race condition for continuation lines when scrolling up */
1120 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1122 (*lineInfo)[m].type = (*lineInfo)[n].type;
1125 /* this also prevents searching through the hidden lines */
1126 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1127 flags = 0; /* M_NOSHOW */
1130 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1131 * don't want to compute it every time M_TYPES is set, since this
1132 * would slow down the "bottom" function unacceptably. A compromise
1133 * solution is hence to call regexec() again, just to find out the
1134 * length of the quote prefix.
1136 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1137 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1140 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1146 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1147 (*lineInfo)[n].quote = classify_quote (QuoteList,
1148 (char *) fmt + pmatch[0].rm_so,
1149 pmatch[0].rm_eo - pmatch[0].rm_so,
1150 force_redraw, q_level);
1153 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1154 && (*lineInfo)[n].search_cnt == -1) {
1156 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1164 (*lineInfo)[n].search_cnt = 0;
1166 (SearchRE, (char *) fmt + offset, 1, pmatch,
1167 (offset ? REG_NOTBOL : 0)) == 0) {
1168 if (++((*lineInfo)[n].search_cnt) > 1)
1169 p_realloc(&(*lineInfo)[n].search, (*lineInfo)[n].search_cnt);
1171 (*lineInfo)[n].search = p_new(struct syntax_t, 1);
1172 pmatch[0].rm_so += offset;
1173 pmatch[0].rm_eo += offset;
1174 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1176 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1179 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1180 offset++; /* avoid degenerate cases */
1182 offset = pmatch[0].rm_eo;
1188 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1189 /* we've already scanned this line, so just exit */
1192 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1193 /* no need to try to display this line... */
1194 return (1); /* fake display */
1197 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1198 sizeof (buf), &buf_ready)) < 0) {
1204 /* now chose a good place to break the line */
1205 cnt = format_line(lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1207 buf_ptr = buf + cnt;
1209 /* move the break point only if smart_wrap is set */
1210 if (option (OPTWRAP)) {
1212 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1213 && buf[cnt] != '\r') {
1215 /* skip trailing blanks */
1216 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1218 /* a very long word with leading spaces causes infinite wrapping */
1219 if ((!ch) && (flags & M_PAGER_NSKIP))
1220 buf_ptr = buf + cnt;
1224 buf_ptr = buf + cnt; /* a very long word... */
1227 if (!(flags & M_PAGER_NSKIP))
1228 /* skip leading blanks on the next line too */
1229 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1233 if (*buf_ptr == '\r')
1235 if (*buf_ptr == '\n')
1238 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1239 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1240 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1242 /* if we don't need to display the line we are done */
1243 if (!(flags & M_SHOW))
1246 /* display the line */
1247 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1249 /* avoid a bug in ncurses... */
1250 #ifndef USE_SLANG_CURSES
1252 SETCOLOR (MT_COLOR_NORMAL);
1257 /* end the last color pattern (needed by S-Lang) */
1258 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1259 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1262 * Fill the blank space at the end of the line with the prevailing color.
1263 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1264 * to make sure to reset the color *after* that
1266 if (flags & M_SHOWCOLOR) {
1267 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1268 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1269 def_color = ((*lineInfo)[m].syntax)[0].color;
1271 def_color = ColorDefs[(*lineInfo)[m].type];
1273 attrset (def_color);
1275 bkgdset (def_color | ' ');
1279 /* ncurses always wraps lines when you get to the right side of the
1280 * screen, but S-Lang seems to only wrap if the next character is *not*
1283 #ifndef USE_SLANG_CURSES
1289 * reset the color back to normal. This *must* come after the
1290 * addch('\n'), otherwise the color for this line will not be
1291 * filled to the right margin.
1293 if (flags & M_SHOWCOLOR) {
1294 SETCOLOR (MT_COLOR_NORMAL);
1295 BKGDSET (MT_COLOR_NORMAL);
1298 /* build a return code */
1299 if (!(flags & M_SHOW))
1305 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1307 while (cur > 0 && nlines > 0) {
1309 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1316 static struct mapping_t PagerHelp[] = {
1317 {N_("Exit"), OP_EXIT},
1318 {N_("PrevPg"), OP_PREV_PAGE},
1319 {N_("NextPg"), OP_NEXT_PAGE},
1322 static struct mapping_t PagerHelpExtra[] = {
1323 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1324 {N_("Del"), OP_DELETE},
1325 {N_("Reply"), OP_REPLY},
1326 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1331 static struct mapping_t PagerNewsHelpExtra[] = {
1332 {N_("Post"), OP_POST},
1333 {N_("Followup"), OP_FOLLOWUP},
1334 {N_("Del"), OP_DELETE},
1335 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1341 /* This pager is actually not so simple as it once was. It now operates in
1342 two modes: one for viewing messages and the other for viewing help. These
1343 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1344 is there so that we can do operations on the current message without the
1345 need to pop back out to the main-menu. */
1347 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1349 static char searchbuf[STRING];
1350 char buffer[LONG_STRING];
1351 char helpstr[STRING * 2];
1352 char tmphelp[STRING * 2];
1353 int maxLine, lastLine = 0;
1354 struct line_t *lineInfo;
1355 struct q_class_t *QuoteList = NULL;
1356 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1357 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1359 int redraw = REDRAW_FULL;
1361 off_t last_pos = 0, last_offset = 0;
1362 int old_smart_wrap, old_markers;
1365 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1366 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1368 int bodyoffset = 1; /* offset of first line of real text */
1369 int statusoffset = 0; /* offset for the status bar */
1370 int helpoffset = LINES - 2; /* offset for the help bar. */
1371 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1373 MUTTMENU *pager_index = NULL; /* the Pager Index (PI) */
1374 int indexoffset = 0; /* offset for the PI */
1375 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1376 int indicator = indexlen / 3; /* the indicator line of the PI */
1377 int old_PagerIndexLines; /* some people want to resize it
1378 * while inside the pager... */
1384 if (!(flags & M_SHOWCOLOR))
1385 flags |= M_SHOWFLAT;
1387 if ((fp = fopen (fname, "r")) == NULL) {
1388 mutt_perror (fname);
1392 if (stat (fname, &sb) != 0) {
1393 mutt_perror (fname);
1399 /* Initialize variables */
1401 if (IsHeader (extra) && !extra->hdr->read) {
1402 Context->msgnotreadyet = extra->hdr->msgno;
1403 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1406 lineInfo = p_new(struct line_t, maxLine = LINES);
1407 for (i = 0; i < maxLine; i++) {
1408 p_clear(&lineInfo[i], 1);
1409 lineInfo[i].type = -1;
1410 lineInfo[i].search_cnt = -1;
1411 lineInfo[i].syntax = p_new(struct syntax_t, 1);
1412 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1415 mutt_compile_help (helpstr, sizeof(helpstr), MENU_PAGER, PagerHelp);
1416 if (IsHeader (extra)) {
1417 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1418 mutt_compile_help (buffer, sizeof(buffer), MENU_PAGER,
1421 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1424 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1427 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1428 mutt_make_help (buffer, sizeof(buffer), _("Help"), MENU_PAGER, OP_HELP);
1429 snprintf (helpstr, sizeof(helpstr), "%s %s", tmphelp, buffer);
1436 if (redraw & REDRAW_FULL) {
1437 SETCOLOR (MT_COLOR_NORMAL);
1438 /* clear() doesn't optimize screen redraws */
1442 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1443 indexlen = Context->vcount + 1;
1445 indexlen = PagerIndexLines;
1447 indicator = indexlen / 3;
1449 if (option (OPTSTATUSONTOP)) {
1451 statusoffset = IsHeader (extra) ? indexlen : 0;
1452 bodyoffset = statusoffset + 1;
1453 helpoffset = LINES - 2;
1454 bodylen = helpoffset - bodyoffset;
1455 if (!option (OPTHELP))
1460 statusoffset = LINES - 2;
1461 if (!option (OPTHELP))
1463 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1464 bodylen = statusoffset - bodyoffset;
1467 if (option (OPTHELP)) {
1468 SETCOLOR (MT_COLOR_STATUS);
1469 move (helpoffset, SW);
1470 mutt_paddstr (COLS-SW, helpstr);
1471 SETCOLOR (MT_COLOR_NORMAL);
1474 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1475 if (Resize != NULL) {
1476 if ((SearchCompiled = Resize->SearchCompiled)) {
1478 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1479 SearchFlag = M_SEARCH;
1480 SearchBack = Resize->SearchBack;
1482 lines = Resize->line;
1483 redraw |= REDRAW_SIGWINCH;
1489 if (IsHeader (extra) && PagerIndexLines) {
1490 if (pager_index == NULL) {
1491 /* only allocate the space if/when we need the index.
1492 Initialise the menu as per the main index */
1493 pager_index = mutt_new_menu ();
1494 pager_index->menu = MENU_MAIN;
1495 pager_index->make_entry = index_make_entry;
1496 pager_index->color = index_color;
1497 pager_index->max = Context->vcount;
1498 pager_index->current = extra->hdr->virtual;
1501 SETCOLOR (MT_COLOR_NORMAL);
1502 pager_index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1504 pager_index->pagelen = indexlen - 1;
1506 /* some fudge to work out where abouts the indicator should go */
1507 if (pager_index->current - indicator < 0)
1508 pager_index->top = 0;
1509 else if (pager_index->max - pager_index->current < pager_index->pagelen - indicator)
1510 pager_index->top = pager_index->max - pager_index->pagelen;
1512 pager_index->top = pager_index->current - indicator;
1514 menu_redraw_index (pager_index);
1517 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1521 if (redraw & REDRAW_SIGWINCH) {
1524 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1525 has_types | SearchFlag, &QuoteList, &q_level,
1526 &force_redraw, &SearchRE) == 0) {
1527 if (!lineInfo[i].continuation && ++j == lines) {
1532 redraw |= REDRAW_SIDEBAR;
1536 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1538 move (bodyoffset, SW);
1539 curline = oldtopline = topline;
1543 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1544 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1546 (flags & M_DISPLAYFLAGS) | hideQuoted |
1547 SearchFlag, &QuoteList, &q_level, &force_redraw,
1551 move (lines + bodyoffset, SW);
1552 redraw |= REDRAW_SIDEBAR;
1554 last_offset = lineInfo[curline].offset;
1555 } while (force_redraw);
1557 SETCOLOR (MT_COLOR_TILDE);
1558 BKGDSET (MT_COLOR_TILDE);
1559 while (lines < bodylen) {
1561 if (option (OPTTILDE))
1565 move (lines + bodyoffset, SW);
1567 /* We are going to update the pager status bar, so it isn't
1568 * necessary to reset to normal color now. */
1570 redraw |= REDRAW_STATUS; /* need to update the % seen */
1573 if (redraw & REDRAW_STATUS) {
1574 /* print out the pager status bar */
1575 SETCOLOR (MT_COLOR_STATUS);
1576 BKGDSET (MT_COLOR_STATUS);
1577 CLEARLINE_WIN (statusoffset);
1578 if (IsHeader (extra)) {
1579 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1580 size_t l2 = sizeof (buffer);
1582 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1583 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1585 else if (IsMsgAttach (extra)) {
1586 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1587 size_t l2 = sizeof (buffer);
1589 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1590 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1592 move(statusoffset,SW);
1593 mutt_paddstr (COLS - 10 - SW, IsHeader (extra) ||
1594 IsMsgAttach (extra) ? buffer : banner);
1597 if (last_pos < sb.st_size - 1)
1598 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1600 addstr (topline == 0 ? "all)" : "end)");
1601 BKGDSET (MT_COLOR_NORMAL);
1602 SETCOLOR (MT_COLOR_NORMAL);
1605 if (redraw & REDRAW_SIDEBAR)
1608 if ((redraw & REDRAW_INDEX) && pager_index) {
1609 /* redraw the pager_index indicator, because the
1610 * flags for this message might have changed. */
1611 menu_redraw_current (pager_index);
1613 /* print out the pager_index status bar */
1614 menu_status_line (buffer, sizeof (buffer), pager_index, NONULL (Status));
1615 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), SW);
1616 SETCOLOR (MT_COLOR_STATUS);
1617 BKGDSET (MT_COLOR_STATUS);
1618 mutt_paddstr (COLS-SW, buffer);
1619 SETCOLOR (MT_COLOR_NORMAL);
1620 BKGDSET (MT_COLOR_NORMAL);
1622 /* if we're not using the index, update every time */
1628 if (option(OPTBRAILLEFRIENDLY)) {
1629 if (brailleLine!=-1) {
1630 move(brailleLine+1, 0);
1634 move (statusoffset, COLS-1);
1637 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1638 && lineInfo[curline].offset < sb.st_size-1) {
1639 if (TopLine - topline > lines)
1648 ch = km_dokey (MENU_PAGER);
1650 mutt_clear_error ();
1657 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1658 else if (SigWinch) {
1659 mutt_resize_screen ();
1661 /* Store current position. */
1663 for (i = 0; i <= topline; i++)
1664 if (!lineInfo[i].continuation)
1667 if (flags & M_PAGER_RETWINCH) {
1668 Resize = p_new(struct resize, 1);
1670 Resize->line = lines;
1671 Resize->SearchCompiled = SearchCompiled;
1672 Resize->SearchBack = SearchBack;
1675 rc = OP_REFORMAT_WINCH;
1677 for (i = 0; i < maxLine; i++) {
1678 lineInfo[i].offset = 0;
1679 lineInfo[i].type = -1;
1680 lineInfo[i].continuation = 0;
1681 lineInfo[i].chunks = 0;
1682 lineInfo[i].search_cnt = -1;
1683 lineInfo[i].quote = NULL;
1685 p_realloc(&lineInfo[i].syntax, 1);
1686 if (SearchCompiled && lineInfo[i].search)
1687 p_delete(&(lineInfo[i].search));
1693 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1698 clearok (stdscr, TRUE); /*force complete redraw */
1702 else if (ch == -1) {
1716 if (lineInfo[curline].offset < sb.st_size - 1) {
1717 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1719 else if (option (OPTPAGERSTOP)) {
1720 /* emulate "less -q" and don't go on to the next message. */
1721 mutt_error _("Bottom of message is shown.");
1723 /* end of the current message, so display the next message. */
1724 rc = OP_MAIN_NEXT_UNDELETED;
1732 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1735 mutt_error _("Top of message is shown.");
1739 if (lineInfo[curline].offset < sb.st_size - 1) {
1742 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1748 mutt_error _("Bottom of message is shown.");
1753 topline = upNLines (1, lineInfo, topline, hideQuoted);
1755 mutt_error _("Top of message is shown.");
1762 mutt_error _("Top of message is shown.");
1767 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1769 mutt_error _("Top of message is shown.");
1773 if (lineInfo[curline].offset < sb.st_size - 1) {
1774 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1776 else if (option (OPTPAGERSTOP)) {
1777 /* emulate "less -q" and don't go on to the next message. */
1778 mutt_error _("Bottom of message is shown.");
1780 /* end of the current message, so display the next message. */
1781 rc = OP_MAIN_NEXT_UNDELETED;
1786 case OP_SEARCH_NEXT:
1787 case OP_SEARCH_OPPOSITE:
1788 if (SearchCompiled) {
1790 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1791 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1792 /* searching forward */
1793 for (i = topline + 1; i < lastLine; i++) {
1794 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1795 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1802 mutt_error _("Not found.");
1805 /* searching backward */
1806 for (i = topline - 1; i >= 0; i--) {
1807 if ((!hideQuoted || (has_types &&
1808 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1809 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1816 mutt_error _("Not found.");
1819 if (lineInfo[topline].search_cnt > 0)
1820 SearchFlag = M_SEARCH;
1824 /* no previous search pattern, so fall through to search */
1827 case OP_SEARCH_REVERSE:
1828 m_strcpy(buffer, sizeof(buffer), searchbuf);
1829 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1830 _("Search: ")), buffer, sizeof (buffer),
1834 if (!m_strcmp (buffer, searchbuf)) {
1835 if (SearchCompiled) {
1836 /* do an implicit search-next */
1837 if (ch == OP_SEARCH)
1838 ch = OP_SEARCH_NEXT;
1840 ch = OP_SEARCH_OPPOSITE;
1849 m_strcpy(searchbuf, sizeof(searchbuf), buffer);
1851 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1852 if (ch == OP_SEARCH)
1854 else if (ch == OP_SEARCH_REVERSE)
1857 if (SearchCompiled) {
1858 regfree (&SearchRE);
1859 for (i = 0; i < lastLine; i++) {
1860 if (lineInfo[i].search)
1861 p_delete(&(lineInfo[i].search));
1862 lineInfo[i].search_cnt = -1;
1867 REGCOMP (&SearchRE, searchbuf,
1868 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1869 regerror (err, &SearchRE, buffer, sizeof (buffer));
1870 mutt_error ("%s", buffer);
1871 regfree (&SearchRE);
1872 for (i = 0; i < maxLine; i++) {
1874 if (lineInfo[i].search)
1875 p_delete(&(lineInfo[i].search));
1876 lineInfo[i].search_cnt = -1;
1883 /* update the search pointers */
1885 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1886 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1887 &QuoteList, &q_level,
1888 &force_redraw, &SearchRE) == 0) {
1890 redraw |= REDRAW_SIDEBAR;
1894 /* searching forward */
1895 for (i = topline; i < lastLine; i++) {
1896 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1897 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1905 /* searching backward */
1906 for (i = topline; i >= 0; i--) {
1907 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1908 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1916 if (lineInfo[topline].search_cnt == 0) {
1918 mutt_error _("Not found.");
1921 SearchFlag = M_SEARCH;
1923 redraw = REDRAW_BODY;
1926 case OP_SEARCH_TOGGLE:
1927 if (SearchCompiled) {
1928 SearchFlag ^= M_SEARCH;
1929 redraw = REDRAW_BODY;
1934 /* don't let the user enter the help-menu from the help screen! */
1937 mutt_help (MENU_PAGER);
1938 redraw = REDRAW_FULL;
1942 mutt_error _("Help is currently being shown.");
1945 case OP_PAGER_HIDE_QUOTED:
1947 hideQuoted ^= M_HIDE;
1948 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1949 topline = upNLines (1, lineInfo, topline, hideQuoted);
1951 redraw = REDRAW_BODY;
1955 case OP_PAGER_SKIP_QUOTED:
1958 int new_topline = topline;
1960 while ((new_topline < lastLine ||
1961 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1962 new_topline, &lastLine,
1963 &maxLine, M_TYPES, &QuoteList,
1964 &q_level, &force_redraw,
1966 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
1967 redraw |= REDRAW_SIDEBAR;
1972 mutt_error _("No more quoted text.");
1977 while ((new_topline < lastLine ||
1978 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1979 new_topline, &lastLine,
1980 &maxLine, M_TYPES, &QuoteList,
1981 &q_level, &force_redraw,
1983 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
1985 redraw |= REDRAW_SIDEBAR;
1989 mutt_error _("No more unquoted text after quoted text.");
1993 topline = new_topline;
1997 case OP_PAGER_BOTTOM: /* move to the end of the file */
1998 if (lineInfo[curline].offset < sb.st_size - 1) {
2000 /* make sure the types are defined to the end of file */
2001 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2002 &maxLine, has_types,
2003 &QuoteList, &q_level, &force_redraw,
2006 redraw |= REDRAW_SIDEBAR;
2008 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2011 mutt_error _("Bottom of message is shown.");
2015 clearok (stdscr, TRUE);
2016 redraw = REDRAW_FULL;
2020 km_error_key (MENU_PAGER);
2023 /* --------------------------------------------------------------------
2024 * The following are operations on the current message rather than
2025 * adjusting the view of the message.
2028 case OP_BOUNCE_MESSAGE:
2029 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2031 if (IsMsgAttach (extra))
2032 mutt_attach_bounce (extra->fp, extra->hdr,
2033 extra->idx, extra->idxlen, extra->bdy);
2035 ci_bounce_message (extra->hdr, &redraw);
2039 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2041 if (IsMsgAttach (extra))
2042 mutt_attach_resend (extra->fp, extra->hdr,
2043 extra->idx, extra->idxlen, extra->bdy);
2045 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2046 redraw = REDRAW_FULL;
2049 case OP_CHECK_TRADITIONAL:
2050 CHECK_MODE (IsHeader (extra));
2051 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2053 rc = OP_CHECK_TRADITIONAL;
2057 case OP_CREATE_ALIAS:
2058 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2059 if (IsMsgAttach (extra))
2060 mutt_create_alias (extra->bdy->hdr->env, NULL);
2062 mutt_create_alias (extra->hdr->env, NULL);
2063 MAYBE_REDRAW (redraw);
2066 case OP_PURGE_MESSAGE:
2068 CHECK_MODE (IsHeader (extra));
2071 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2073 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2074 mutt_set_flag (Context, extra->hdr, M_PURGED,
2075 ch != OP_PURGE_MESSAGE ? 0 : 1);
2076 if (option (OPTDELETEUNTAG))
2077 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2078 redraw = REDRAW_STATUS | REDRAW_INDEX;
2079 if (option (OPTRESOLVE)) {
2081 rc = OP_MAIN_NEXT_UNDELETED;
2085 case OP_DELETE_THREAD:
2086 case OP_DELETE_SUBTHREAD:
2087 CHECK_MODE (IsHeader (extra));
2090 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2092 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2093 ch == OP_DELETE_THREAD ? 0 : 1);
2096 if (option (OPTDELETEUNTAG))
2097 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2098 ch == OP_DELETE_THREAD ? 0 : 1);
2099 if (option (OPTRESOLVE)) {
2100 rc = OP_MAIN_NEXT_UNDELETED;
2104 if (!option (OPTRESOLVE) && PagerIndexLines)
2105 redraw = REDRAW_FULL;
2107 redraw = REDRAW_STATUS | REDRAW_INDEX;
2111 case OP_DISPLAY_ADDRESS:
2112 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2113 if (IsMsgAttach (extra))
2114 mutt_display_address (extra->bdy->hdr->env);
2116 mutt_display_address (extra->hdr->env);
2119 case OP_ENTER_COMMAND:
2120 old_smart_wrap = option (OPTWRAP);
2121 old_markers = option (OPTMARKERS);
2122 old_PagerIndexLines = PagerIndexLines;
2124 CurrentMenu = MENU_PAGER;
2125 mutt_enter_command ();
2127 if (option (OPTNEEDRESORT)) {
2128 unset_option (OPTNEEDRESORT);
2129 CHECK_MODE (IsHeader (extra));
2130 set_option (OPTNEEDRESORT);
2133 if (old_PagerIndexLines != PagerIndexLines) {
2135 mutt_menuDestroy (&pager_index);
2139 if (option (OPTWRAP) != old_smart_wrap ||
2140 option (OPTMARKERS) != old_markers) {
2141 if (flags & M_PAGER_RETWINCH) {
2143 rc = OP_REFORMAT_WINCH;
2147 /* count the real lines above */
2149 for (i = 0; i <= topline; i++) {
2150 if (!lineInfo[i].continuation)
2154 /* we need to restart the whole thing */
2155 for (i = 0; i < maxLine; i++) {
2156 lineInfo[i].offset = 0;
2157 lineInfo[i].type = -1;
2158 lineInfo[i].continuation = 0;
2159 lineInfo[i].chunks = 0;
2160 lineInfo[i].search_cnt = -1;
2161 lineInfo[i].quote = NULL;
2163 p_realloc(&(lineInfo[i].syntax), 1);
2164 if (SearchCompiled && lineInfo[i].search)
2165 p_delete(&(lineInfo[i].search));
2168 if (SearchCompiled) {
2169 regfree (&SearchRE);
2174 /* try to keep the old position */
2177 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2178 &lastLine, &maxLine,
2179 (has_types ? M_TYPES : 0),
2180 &QuoteList, &q_level, &force_redraw,
2182 redraw |= REDRAW_SIDEBAR;
2183 if (!lineInfo[topline].continuation)
2192 if (option (OPTFORCEREDRAWPAGER))
2193 redraw = REDRAW_FULL;
2194 unset_option (OPTFORCEREDRAWINDEX);
2195 unset_option (OPTFORCEREDRAWPAGER);
2198 case OP_FLAG_MESSAGE:
2199 CHECK_MODE (IsHeader (extra));
2202 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2204 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2205 redraw = REDRAW_STATUS | REDRAW_INDEX;
2206 if (option (OPTRESOLVE)) {
2208 rc = OP_MAIN_NEXT_UNDELETED;
2213 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2214 if (IsAttach (extra))
2215 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2217 mutt_pipe_message (extra->hdr);
2218 MAYBE_REDRAW (redraw);
2222 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2223 if (IsAttach (extra))
2224 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2226 mutt_print_message (extra->hdr);
2230 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2232 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2233 redraw = REDRAW_FULL;
2238 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2240 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2241 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2242 query_quadoption (OPT_TOMODERATED,
2244 ("Posting to this group not allowed, may be moderated. Continue?"))
2247 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2248 redraw = REDRAW_FULL;
2251 case OP_FORWARD_TO_GROUP:
2252 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2254 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2255 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2256 query_quadoption (OPT_TOMODERATED,
2258 ("Posting to this group not allowed, may be moderated. Continue?"))
2261 if (IsMsgAttach (extra))
2262 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2263 extra->idxlen, extra->bdy, SENDNEWS);
2265 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2267 redraw = REDRAW_FULL;
2271 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2274 if (IsMsgAttach (extra))
2275 followup_to = extra->bdy->hdr->env->followup_to;
2277 followup_to = extra->hdr->env->followup_to;
2279 if (!followup_to || m_strcasecmp(followup_to, "poster") ||
2280 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2281 _("Reply by mail as poster prefers?")) != M_YES) {
2282 if (extra->ctx && extra->ctx->magic == M_NNTP
2283 && !((NNTP_DATA *) extra->ctx->data)->allowed
2284 && query_quadoption (OPT_TOMODERATED,
2286 ("Posting to this group not allowed, may be moderated. Continue?"))
2289 if (IsMsgAttach (extra))
2290 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2291 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2293 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2294 extra->ctx, extra->hdr);
2295 redraw = REDRAW_FULL;
2301 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2303 if (IsMsgAttach (extra))
2304 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2305 extra->idxlen, extra->bdy, SENDREPLY);
2307 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2308 redraw = REDRAW_FULL;
2311 case OP_RECALL_MESSAGE:
2312 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2314 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2315 redraw = REDRAW_FULL;
2318 case OP_GROUP_REPLY:
2319 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2321 if (IsMsgAttach (extra))
2322 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2323 extra->idxlen, extra->bdy,
2324 SENDREPLY | SENDGROUPREPLY);
2326 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2328 redraw = REDRAW_FULL;
2332 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2334 if (IsMsgAttach (extra))
2335 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2336 extra->idxlen, extra->bdy,
2337 SENDREPLY | SENDLISTREPLY);
2339 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2341 redraw = REDRAW_FULL;
2344 case OP_FORWARD_MESSAGE:
2345 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2347 if (IsMsgAttach (extra))
2348 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2349 extra->idxlen, extra->bdy, 0);
2351 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2352 redraw = REDRAW_FULL;
2355 case OP_DECRYPT_SAVE:
2357 if (IsAttach (extra)) {
2358 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2363 case OP_COPY_MESSAGE:
2364 case OP_DECODE_SAVE:
2365 case OP_DECODE_COPY:
2366 case OP_DECRYPT_COPY:
2367 CHECK_MODE (IsHeader (extra));
2368 if (mutt_save_message (extra->hdr,
2369 (ch == OP_DECRYPT_SAVE) ||
2370 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2371 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2372 (ch == OP_DECRYPT_SAVE)
2373 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2374 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2375 || ch == OP_DECRYPT_SAVE)) {
2376 if (option (OPTRESOLVE)) {
2378 rc = OP_MAIN_NEXT_UNDELETED;
2381 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2383 MAYBE_REDRAW (redraw);
2386 case OP_SHELL_ESCAPE:
2387 mutt_shell_escape ();
2388 MAYBE_REDRAW (redraw);
2392 CHECK_MODE (IsHeader (extra));
2393 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2395 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2396 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2397 ? NULL : Context->last_tag);
2399 redraw = REDRAW_STATUS | REDRAW_INDEX;
2400 if (option (OPTRESOLVE)) {
2407 CHECK_MODE (IsHeader (extra));
2410 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2412 if (extra->hdr->read || extra->hdr->old)
2413 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2415 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2417 Context->msgnotreadyet = -1;
2418 redraw = REDRAW_STATUS | REDRAW_INDEX;
2419 if (option (OPTRESOLVE)) {
2421 rc = OP_MAIN_NEXT_UNDELETED;
2426 CHECK_MODE (IsHeader (extra));
2429 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2431 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2432 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2433 redraw = REDRAW_STATUS | REDRAW_INDEX;
2434 if (option (OPTRESOLVE)) {
2440 case OP_UNDELETE_THREAD:
2441 case OP_UNDELETE_SUBTHREAD:
2442 CHECK_MODE (IsHeader (extra));
2445 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2447 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2448 ch == OP_UNDELETE_THREAD ? 0 : 1)
2449 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2450 ch == OP_UNDELETE_THREAD ? 0 : 1);
2453 if (option (OPTRESOLVE)) {
2454 rc = (ch == OP_DELETE_THREAD) ?
2455 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2459 if (!option (OPTRESOLVE) && PagerIndexLines)
2460 redraw = REDRAW_FULL;
2462 redraw = REDRAW_STATUS | REDRAW_INDEX;
2471 if (option (OPTFORCEBUFFYCHECK))
2474 redraw |= REDRAW_SIDEBAR;
2477 case OP_VIEW_ATTACHMENTS:
2478 if (flags & M_PAGER_ATTACHMENT) {
2480 rc = OP_ATTACH_COLLAPSE;
2483 CHECK_MODE (IsHeader (extra));
2484 mutt_view_attachments (extra->hdr);
2485 if (extra->hdr->attach_del)
2486 Context->changed = 1;
2487 redraw = REDRAW_FULL;
2492 CHECK_MODE (IsHeader (extra));
2494 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2495 redraw = REDRAW_FULL;
2499 case OP_FORGET_PASSPHRASE:
2500 crypt_forget_passphrase ();
2503 case OP_EXTRACT_KEYS:
2504 CHECK_MODE (IsHeader (extra));
2505 crypt_extract_keys_from_messages (extra->hdr);
2506 redraw = REDRAW_FULL;
2509 case OP_SIDEBAR_SCROLL_UP:
2510 case OP_SIDEBAR_SCROLL_DOWN:
2511 case OP_SIDEBAR_NEXT:
2512 case OP_SIDEBAR_NEXT_NEW:
2513 case OP_SIDEBAR_PREV:
2514 case OP_SIDEBAR_PREV_NEW:
2515 sidebar_scroll (ch);
2524 if (IsHeader (extra)) {
2525 Context->msgnotreadyet = -1;
2530 OldHdr = extra->hdr;
2534 cleanup_quote (&QuoteList);
2536 for (i = 0; i < maxLine; i++) {
2537 p_delete(&(lineInfo[i].syntax));
2538 if (SearchCompiled && lineInfo[i].search)
2539 p_delete(&(lineInfo[i].search));
2541 if (SearchCompiled) {
2542 regfree (&SearchRE);
2545 p_delete(&lineInfo);
2547 mutt_menuDestroy (&pager_index);
2548 return (rc != -1 ? rc : 0);