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.
20 #include "mutt_curses.h"
22 #include "mutt_menu.h"
27 #include "recvattach.h"
35 #include "imap_private.h"
38 #include "mutt_crypt.h"
44 #include "lib/debug.h"
54 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
56 #define IsAttach(x) (x && (x)->bdy)
57 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
58 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
59 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
60 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
61 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
63 static const char *Not_available_in_this_menu =
64 N_("Not available in this menu.");
65 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
66 static const char *Function_not_permitted_in_attach_message_mode =
67 N_("Function not permitted in attach-message mode.");
69 /* hack to return to position when returning from index to same message */
70 static int TopLine = 0;
71 static HEADER *OldHdr = NULL;
73 #define CHECK_MODE(x) if (!(x)) \
76 mutt_error _(Not_available_in_this_menu); \
80 #define CHECK_READONLY if (Context->readonly) \
83 mutt_error _(Mailbox_is_read_only); \
87 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
90 mutt_error _(Function_not_permitted_in_attach_message_mode); \
99 struct q_class_t *next, *prev;
100 struct q_class_t *down, *up;
115 struct syntax_t *syntax;
116 struct syntax_t *search;
117 struct q_class_t *quote;
120 #define ANSI_OFF (1<<0)
121 #define ANSI_BLINK (1<<1)
122 #define ANSI_BOLD (1<<2)
123 #define ANSI_UNDERLINE (1<<3)
124 #define ANSI_REVERSE (1<<4)
125 #define ANSI_COLOR (1<<5)
127 typedef struct _ansi_attr {
134 static short InHelp = 0;
136 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
137 static struct resize {
144 #define NumSigLines 4
146 static int check_sig (const char *s, struct line_t *info, int n)
150 while (n > 0 && count <= NumSigLines) {
151 if (info[n].type != MT_COLOR_SIGNATURE)
160 if (count > NumSigLines) {
161 /* check for a blank line */
175 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
176 int special, ansi_attr * a)
178 int def_color; /* color without syntax hilight */
179 int color; /* final color */
180 static int last_color; /* last color set */
181 int search = 0, i, m;
184 last_color = -1; /* force attrset() */
186 if (lineInfo[n].continuation) {
187 if (!cnt && option (OPTMARKERS)) {
188 SETCOLOR (MT_COLOR_MARKERS);
190 last_color = ColorDefs[MT_COLOR_MARKERS];
192 m = (lineInfo[n].syntax)[0].first;
193 cnt += (lineInfo[n].syntax)[0].last;
197 if (!(flags & M_SHOWCOLOR))
198 def_color = ColorDefs[MT_COLOR_NORMAL];
199 else if (lineInfo[m].type == MT_COLOR_HEADER)
200 def_color = (lineInfo[m].syntax)[0].color;
202 def_color = ColorDefs[lineInfo[m].type];
204 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
205 struct q_class_t *class = lineInfo[m].quote;
208 def_color = class->color;
210 while (class && class->length > cnt) {
211 def_color = class->color;
218 if (flags & M_SHOWCOLOR) {
219 for (i = 0; i < lineInfo[m].chunks; i++) {
220 /* we assume the chunks are sorted */
221 if (cnt > (lineInfo[m].syntax)[i].last)
223 if (cnt < (lineInfo[m].syntax)[i].first)
225 if (cnt != (lineInfo[m].syntax)[i].last) {
226 color = (lineInfo[m].syntax)[i].color;
229 /* don't break here, as cnt might be
230 * in the next chunk as well */
234 if (flags & M_SEARCH) {
235 for (i = 0; i < lineInfo[m].search_cnt; i++) {
236 if (cnt > (lineInfo[m].search)[i].last)
238 if (cnt < (lineInfo[m].search)[i].first)
240 if (cnt != (lineInfo[m].search)[i].last) {
241 color = ColorDefs[MT_COLOR_SEARCH];
248 /* handle "special" bold & underlined characters */
249 if (special || a->attr) {
251 if ((a->attr & ANSI_COLOR)) {
253 a->pair = mutt_alloc_color (a->fg, a->bg);
255 if (a->attr & ANSI_BOLD)
260 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
261 if (ColorDefs[MT_COLOR_BOLD] && !search)
262 color = ColorDefs[MT_COLOR_BOLD];
266 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
267 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
268 color = ColorDefs[MT_COLOR_UNDERLINE];
270 color ^= A_UNDERLINE;
272 else if (a->attr & ANSI_REVERSE) {
275 else if (a->attr & ANSI_BLINK) {
278 else if (a->attr & ANSI_OFF) {
283 if (color != last_color) {
289 static void append_line (struct line_t *lineInfo, int n, int cnt)
293 lineInfo[n + 1].type = lineInfo[n].type;
294 (lineInfo[n + 1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
295 lineInfo[n + 1].continuation = 1;
297 /* find the real start of the line */
298 for (m = n; m >= 0; m--)
299 if (lineInfo[m].continuation == 0)
302 (lineInfo[n + 1].syntax)[0].first = m;
303 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
304 cnt + (lineInfo[n].syntax)[0].last : cnt;
307 static void new_class_color (struct q_class_t *class, int *q_level)
309 class->index = (*q_level)++;
310 class->color = ColorQuote[class->index % ColorQuoteUsed];
314 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
315 int index, int *q_level)
317 struct q_class_t *q_list;
320 new_class->index = -1;
323 if (q_list->index >= index) {
325 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
328 q_list = q_list->down;
329 else if (q_list->next)
330 q_list = q_list->next;
332 while (!q_list->next) {
338 q_list = q_list->next;
342 new_class->index = index;
343 new_class->color = ColorQuote[index % ColorQuoteUsed];
347 static void cleanup_quote (struct q_class_t **QuoteList)
349 struct q_class_t *ptr;
352 if ((*QuoteList)->down)
353 cleanup_quote (&((*QuoteList)->down));
354 ptr = (*QuoteList)->next;
355 if ((*QuoteList)->prefix)
356 mem_free (&(*QuoteList)->prefix);
357 mem_free (QuoteList);
364 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
365 const char *qptr, int length,
366 int *force_redraw, int *q_level)
368 struct q_class_t *q_list = *QuoteList;
369 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
371 int offset, tail_lng;
374 if (ColorQuoteUsed <= 1) {
375 /* not much point in classifying quotes... */
377 if (*QuoteList == NULL) {
378 class = (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
379 class->color = ColorQuote[0];
385 /* Did I mention how much I like emulating Lisp in C? */
387 /* classify quoting prefix */
389 if (length <= q_list->length) {
390 /* case 1: check the top level nodes */
392 if (str_ncmp (qptr, q_list->prefix, length) == 0) {
393 if (length == q_list->length)
394 return q_list; /* same prefix: return the current class */
396 /* found shorter prefix */
398 /* add a node above q_list */
400 (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
401 tmp->prefix = (char *) mem_calloc (1, length + 1);
402 strncpy (tmp->prefix, qptr, length);
403 tmp->length = length;
405 /* replace q_list by tmp in the top level list */
407 tmp->next = q_list->next;
408 q_list->next->prev = tmp;
411 tmp->prev = q_list->prev;
412 q_list->prev->next = tmp;
415 /* make q_list a child of tmp */
419 /* q_list has no siblings for now */
423 /* update the root if necessary */
424 if (q_list == *QuoteList)
427 index = q_list->index;
429 /* tmp should be the return class too */
432 /* next class to test; if tmp is a shorter prefix for another
433 * node, that node can only be in the top level list, so don't
434 * go down after this point
439 /* found another branch for which tmp is a shorter prefix */
441 /* save the next sibling for later */
444 /* unlink q_list from the top level list */
446 q_list->next->prev = q_list->prev;
448 q_list->prev->next = q_list->next;
450 /* at this point, we have a tmp->down; link q_list to it */
452 /* sibling order is important here, q_list should be linked last */
460 index = q_list->index;
462 /* next class to test; as above, we shouldn't go down */
466 /* we found a shorter prefix, so certain quotes have changed classes */
471 /* shorter, but not a substring of the current class: try next */
472 q_list = q_list->next;
477 /* case 2: try subclassing the current top level node */
479 /* tmp != NULL means we already found a shorter prefix at case 1 */
481 && str_ncmp (qptr, q_list->prefix, q_list->length) == 0) {
482 /* ok, it's a subclass somewhere on this branch */
485 offset = q_list->length;
487 q_list = q_list->down;
488 tail_lng = length - offset;
489 tail_qptr = (char *) qptr + offset;
492 if (length <= q_list->length) {
493 if (str_ncmp (tail_qptr, (q_list->prefix) + offset, tail_lng)
495 /* same prefix: return the current class */
496 if (length == q_list->length)
499 /* found shorter common prefix */
501 /* add a node above q_list */
502 tmp = (struct q_class_t *) mem_calloc (1,
505 tmp->prefix = (char *) mem_calloc (1, length + 1);
506 strncpy (tmp->prefix, qptr, length);
507 tmp->length = length;
509 /* replace q_list by tmp */
511 tmp->next = q_list->next;
512 q_list->next->prev = tmp;
515 tmp->prev = q_list->prev;
516 q_list->prev->next = tmp;
519 /* make q_list a child of tmp */
521 tmp->up = q_list->up;
523 if (tmp->up->down == q_list)
526 /* q_list has no siblings */
530 index = q_list->index;
532 /* tmp should be the return class too */
535 /* next class to test */
539 /* found another branch for which tmp is a shorter prefix */
541 /* save the next sibling for later */
544 /* unlink q_list from the top level list */
546 q_list->next->prev = q_list->prev;
548 q_list->prev->next = q_list->next;
550 /* at this point, we have a tmp->down; link q_list to it */
559 index = q_list->index;
561 /* next class to test */
565 /* we found a shorter prefix, so we need a redraw */
570 q_list = q_list->next;
575 /* longer than the current prefix: try subclassing it */
577 && str_ncmp (tail_qptr, (q_list->prefix) + offset,
578 q_list->length - offset) == 0) {
579 /* still a subclass: go down one level */
581 offset = q_list->length;
583 q_list = q_list->down;
584 tail_lng = length - offset;
585 tail_qptr = (char *) qptr + offset;
590 /* nope, try the next prefix */
591 q_list = q_list->next;
597 /* still not found so far: add it as a sibling to the current node */
600 (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
601 tmp->prefix = (char *) mem_calloc (1, length + 1);
602 strncpy (tmp->prefix, qptr, length);
603 tmp->length = length;
606 tmp->next = ptr->down;
607 ptr->down->prev = tmp;
612 new_class_color (tmp, q_level);
618 shift_class_colors (*QuoteList, tmp, index, q_level);
624 /* nope, try the next prefix */
625 q_list = q_list->next;
632 /* not found so far: add it as a top level class */
633 class = (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
634 class->prefix = (char *) mem_calloc (1, length + 1);
635 strncpy (class->prefix, qptr, length);
636 class->length = length;
637 new_class_color (class, q_level);
640 class->next = *QuoteList;
641 (*QuoteList)->prev = class;
647 shift_class_colors (*QuoteList, tmp, index, q_level);
652 static int brailleLine = -1;
653 static int brailleCol = -1;
655 static int check_attachment_marker (char *);
658 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
659 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
662 COLOR_LINE *color_line;
663 regmatch_t pmatch[1], smatch[1];
664 int found, offset, null_rx, i;
666 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
667 if (buf[0] == '\n') {
668 lineInfo[n].type = MT_COLOR_NORMAL;
669 getyx(stdscr, brailleLine, brailleCol);
671 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
672 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
673 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
676 lineInfo[n].type = MT_COLOR_HDEFAULT;
677 color_line = ColorHdrList;
679 if (REGEXEC (&color_line->rx, buf) == 0) {
680 lineInfo[n].type = MT_COLOR_HEADER;
681 lineInfo[n].syntax[0].color = color_line->pair;
684 color_line = color_line->next;
688 else if (str_ncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
689 lineInfo[n].type = MT_COLOR_NORMAL;
691 else if (str_ncmp ("[-- ", buf, 4) == 0)
692 lineInfo[n].type = MT_COLOR_ATTACHMENT;
694 else if (check_attachment_marker ((char *) raw) == 0)
695 lineInfo[n].type = MT_COLOR_ATTACHMENT;
697 else if (str_cmp ("-- \n", buf) == 0
698 || str_cmp ("-- \r\n", buf) == 0) {
701 lineInfo[n].type = MT_COLOR_SIGNATURE;
702 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
703 (lineInfo[i].type == MT_COLOR_NORMAL ||
704 lineInfo[i].type == MT_COLOR_QUOTED ||
705 lineInfo[i].type == MT_COLOR_HEADER)) {
707 if (lineInfo[i].chunks) {
708 lineInfo[i].chunks = 0;
709 mem_realloc (&(lineInfo[n].syntax), sizeof (struct syntax_t));
711 lineInfo[i++].type = MT_COLOR_SIGNATURE;
714 else if (check_sig (buf, lineInfo, n - 1) == 0)
715 lineInfo[n].type = MT_COLOR_SIGNATURE;
716 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
717 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
718 if (smatch[0].rm_so > 0) {
721 /* hack to avoid making an extra copy of buf */
722 c = buf[smatch[0].rm_so];
723 buf[smatch[0].rm_so] = 0;
725 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
726 if (q_classify && lineInfo[n].quote == NULL)
727 lineInfo[n].quote = classify_quote (QuoteList,
728 buf + pmatch[0].rm_so,
730 pmatch[0].rm_so, force_redraw,
732 lineInfo[n].type = MT_COLOR_QUOTED;
735 lineInfo[n].type = MT_COLOR_NORMAL;
737 buf[smatch[0].rm_so] = c;
740 lineInfo[n].type = MT_COLOR_NORMAL;
743 if (q_classify && lineInfo[n].quote == NULL)
744 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
745 pmatch[0].rm_eo - pmatch[0].rm_so,
746 force_redraw, q_level);
747 lineInfo[n].type = MT_COLOR_QUOTED;
751 lineInfo[n].type = MT_COLOR_NORMAL;
754 if (lineInfo[n].type == MT_COLOR_NORMAL ||
755 lineInfo[n].type == MT_COLOR_QUOTED) {
759 lineInfo[n].chunks = 0;
766 color_line = ColorBodyList;
768 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
769 (offset ? REG_NOTBOL : 0)) == 0) {
770 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
772 if (++(lineInfo[n].chunks) > 1)
773 mem_realloc (&(lineInfo[n].syntax),
774 (lineInfo[n].chunks) *
775 sizeof (struct syntax_t));
777 i = lineInfo[n].chunks - 1;
778 pmatch[0].rm_so += offset;
779 pmatch[0].rm_eo += offset;
781 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
782 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
783 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
784 (lineInfo[n].syntax)[i].color = color_line->pair;
785 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
786 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
792 null_rx = 1; /* empty regexp; don't add it, but keep looking */
794 color_line = color_line->next;
798 offset++; /* avoid degenerate cases */
800 offset = (lineInfo[n].syntax)[i].last;
801 } while (found || null_rx);
805 static int is_ansi (unsigned char *buf)
807 while (*buf && (isdigit (*buf) || *buf == ';'))
809 return (*buf == 'm');
812 static int check_attachment_marker (char *p)
814 char *q = AttachmentMarker;
816 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
817 return (int) (*p - *q);
820 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
824 while (isdigit (buf[x]) || buf[x] == ';')
827 /* Character Attributes */
828 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
832 mutt_free_color (a->fg, a->bg);
840 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
841 a->attr |= ANSI_BOLD;
844 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
845 a->attr |= ANSI_UNDERLINE;
848 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
849 a->attr |= ANSI_BLINK;
852 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
853 a->attr |= ANSI_REVERSE;
856 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
861 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
864 mutt_free_color (a->fg, a->bg);
867 a->attr |= ANSI_COLOR;
868 if (buf[pos + 1] != '9')
869 a->fg = buf[pos + 1] - '0';
872 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
875 mutt_free_color (a->fg, a->bg);
878 a->attr |= ANSI_COLOR;
879 if (buf[pos + 1] != '9')
880 a->bg = buf[pos + 1] - '0';
884 while (pos < x && buf[pos] != ';')
894 /* trim tail of buf so that it contains complete multibyte characters */
895 static int trim_incomplete_mbyte(unsigned char *buf, size_t len) {
899 memset (&mbstate, 0, sizeof (mbstate));
900 for (; len > 0; buf += k, len -= k) {
901 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
904 else if (k == -1 || k == 0)
913 fill_buffer (FILE * f, off_t *last_pos, off_t offset, unsigned char *buf,
914 unsigned char *fmt, size_t blen, int *buf_ready)
917 static int b_read = 0;
919 if (*buf_ready == 0) {
921 if (offset != *last_pos)
922 fseeko (f, offset, 0);
923 if (fgets ((char *) buf, blen - 1, f) == NULL) {
927 *last_pos = ftello (f);
928 b_read = (int) (*last_pos - offset);
931 /* incomplete mbyte characters trigger a segfault in regex processing for
932 * certain versions of glibc. Trim them if necessary. */
933 if (b_read == blen - 2)
934 b_read -= trim_incomplete_mbyte(buf, b_read);
936 /* copy "buf" to "fmt", but without bold and underline controls */
939 if (*p == '\010' && (p > buf)) {
940 if (*(p + 1) == '_') /* underline */
942 else if (*(p + 1)) { /* bold or overstrike */
943 *(fmt - 1) = *(p + 1);
949 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
950 while (*p++ != 'm') /* skip ANSI sequence */
953 else if (*p == '\033' && *(p + 1) == ']'
954 && check_attachment_marker ((char *) p) == 0) {
955 debug_print (2, ("seen attachment marker.\n"));
956 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
973 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
974 int flags, ansi_attr * pa, int cnt,
975 int *pspace, int *pvch, int *pcol, int *pspecial)
977 int space = -1; /* index of the last space or TAB */
978 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
979 int ch, vch, k, last_special = -1, special = 0, t;
983 int wrap_cols = COLS;
985 if (!(flags & (M_SHOWFLAT)))
986 wrap_cols -= WrapMargin;
992 /* FIXME: this should come from lineInfo */
993 memset (&mbstate, 0, sizeof (mbstate));
995 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
996 /* Handle ANSI sequences */
997 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
998 is_ansi (buf + ch + 2))
999 ch = grok_ansi (buf, ch + 2, pa) + 1;
1001 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
1002 check_attachment_marker ((char *) buf + ch) == 0) {
1003 while (buf[ch++] != '\a')
1008 /* is anything left to do? */
1012 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
1013 if (k == -2 || k == -1) {
1014 debug_print (1, ("mbrtowc returned %d; errno = %d.\n", k, errno));
1015 if (col + 4 > wrap_cols)
1019 printw ("\\%03o", buf[ch]);
1026 /* Handle backspace */
1028 if (IsWPrint (wc)) {
1033 while ((wc1 = 0, mbstate1 = mbstate,
1035 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
1036 &mbstate1), k1 - k > 0 && wc1 == '\b')
1038 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1039 &mbstate1), k2 > 0 && IsWPrint (wc1))) {
1041 special |= (wc == '_' && special & A_UNDERLINE)
1042 ? A_UNDERLINE : A_BOLD;
1044 else if (wc == '_' || wc1 == '_') {
1045 special |= A_UNDERLINE;
1046 wc = (wc1 == '_') ? wc : wc1;
1049 /* special = 0; / * overstrike: nothing to do! */
1059 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1060 special || last_special || pa->attr)) {
1061 resolve_color (*lineInfo, n, vch, flags, special, pa);
1062 last_special = special;
1065 if (IsWPrint (wc)) {
1069 if (col + t > wrap_cols)
1075 else if (wc == '\n')
1077 else if (wc == '\t') {
1083 for (; col < t; col++)
1088 else if (wc < 0x20 || wc == 0x7f) {
1089 if (col + 2 > wrap_cols)
1093 printw ("^%c", ('@' + wc) & 0x7f);
1095 else if (wc < 0x100) {
1096 if (col + 4 > wrap_cols)
1100 printw ("\\%03o", wc);
1103 if (col + 1 > wrap_cols)
1107 addch (replacement_char ());
1113 *pspecial = special;
1119 * flags M_SHOWFLAT, show characters (used for displaying help)
1120 * M_SHOWCOLOR, show characters in color
1121 * otherwise don't show characters
1122 * M_HIDE, don't show quoted text
1123 * M_SEARCH, resolve search patterns
1124 * M_TYPES, compute line's type
1125 * M_PAGER_NSKIP, keeps leading whitespace
1126 * M_PAGER_MARKER, eventually show markers
1129 * -1 EOF was reached
1130 * 0 normal exit, line was not displayed
1131 * >0 normal exit, line was displayed
1135 display_line (FILE * f, off_t *last_pos, struct line_t **lineInfo, int n,
1136 int *last, int *max, int flags, struct q_class_t **QuoteList,
1137 int *q_level, int *force_redraw, regex_t * SearchRE)
1139 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1140 unsigned char *buf_ptr = buf;
1141 int ch, vch, col, cnt, b_read;
1142 int buf_ready = 0, change_last = 0;
1147 ansi_attr a = { 0, 0, 0, -1 };
1148 regmatch_t pmatch[1];
1155 if (*last == *max) {
1156 mem_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1157 for (ch = *last; ch < *max; ch++) {
1158 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1159 (*lineInfo)[ch].type = -1;
1160 (*lineInfo)[ch].search_cnt = -1;
1161 (*lineInfo)[ch].syntax = mem_malloc (sizeof (struct syntax_t));
1162 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1167 /* only do color hiliting if we are viewing a message */
1168 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1169 if ((*lineInfo)[n].type == -1) {
1170 /* determine the line class */
1172 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1179 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1180 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1182 /* avoid race condition for continuation lines when scrolling up */
1184 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1186 (*lineInfo)[m].type = (*lineInfo)[n].type;
1189 /* this also prevents searching through the hidden lines */
1190 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1191 flags = 0; /* M_NOSHOW */
1194 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1195 * don't want to compute it every time M_TYPES is set, since this
1196 * would slow down the "bottom" function unacceptably. A compromise
1197 * solution is hence to call regexec() again, just to find out the
1198 * length of the quote prefix.
1200 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1201 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1204 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1210 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1211 (*lineInfo)[n].quote = classify_quote (QuoteList,
1212 (char *) fmt + pmatch[0].rm_so,
1213 pmatch[0].rm_eo - pmatch[0].rm_so,
1214 force_redraw, q_level);
1217 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1218 && (*lineInfo)[n].search_cnt == -1) {
1220 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1228 (*lineInfo)[n].search_cnt = 0;
1230 (SearchRE, (char *) fmt + offset, 1, pmatch,
1231 (offset ? REG_NOTBOL : 0)) == 0) {
1232 if (++((*lineInfo)[n].search_cnt) > 1)
1233 mem_realloc (&((*lineInfo)[n].search),
1234 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1236 (*lineInfo)[n].search = mem_malloc (sizeof (struct syntax_t));
1237 pmatch[0].rm_so += offset;
1238 pmatch[0].rm_eo += offset;
1239 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1241 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1244 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1245 offset++; /* avoid degenerate cases */
1247 offset = pmatch[0].rm_eo;
1253 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1254 /* we've already scanned this line, so just exit */
1257 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1258 /* no need to try to display this line... */
1259 return (1); /* fake display */
1262 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1263 sizeof (buf), &buf_ready)) < 0) {
1269 /* now chose a good place to break the line */
1271 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1273 buf_ptr = buf + cnt;
1275 /* move the break point only if smart_wrap is set */
1276 if (option (OPTWRAP)) {
1278 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1279 && buf[cnt] != '\r') {
1281 /* skip trailing blanks */
1282 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1284 /* a very long word with leading spaces causes infinite wrapping */
1285 if ((!ch) && (flags & M_PAGER_NSKIP))
1286 buf_ptr = buf + cnt;
1291 buf_ptr = buf + cnt; /* a very long word... */
1293 if (!(flags & M_PAGER_NSKIP))
1294 /* skip leading blanks on the next line too */
1295 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1299 if (*buf_ptr == '\r')
1301 if (*buf_ptr == '\n')
1304 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1305 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1306 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1308 /* if we don't need to display the line we are done */
1309 if (!(flags & M_SHOW))
1312 /* display the line */
1313 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1315 /* avoid a bug in ncurses... */
1316 #ifndef USE_SLANG_CURSES
1318 SETCOLOR (MT_COLOR_NORMAL);
1323 /* end the last color pattern (needed by S-Lang) */
1324 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1325 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1328 * Fill the blank space at the end of the line with the prevailing color.
1329 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1330 * to make sure to reset the color *after* that
1332 if (flags & M_SHOWCOLOR) {
1333 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1334 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1335 def_color = ((*lineInfo)[m].syntax)[0].color;
1337 def_color = ColorDefs[(*lineInfo)[m].type];
1339 attrset (def_color);
1341 bkgdset (def_color | ' ');
1345 /* ncurses always wraps lines when you get to the right side of the
1346 * screen, but S-Lang seems to only wrap if the next character is *not*
1349 #ifndef USE_SLANG_CURSES
1355 * reset the color back to normal. This *must* come after the
1356 * addch('\n'), otherwise the color for this line will not be
1357 * filled to the right margin.
1359 if (flags & M_SHOWCOLOR) {
1360 SETCOLOR (MT_COLOR_NORMAL);
1361 BKGDSET (MT_COLOR_NORMAL);
1364 /* build a return code */
1365 if (!(flags & M_SHOW))
1371 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1373 while (cur > 0 && nlines > 0) {
1375 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1382 static struct mapping_t PagerHelp[] = {
1383 {N_("Exit"), OP_EXIT},
1384 {N_("PrevPg"), OP_PREV_PAGE},
1385 {N_("NextPg"), OP_NEXT_PAGE},
1388 static struct mapping_t PagerHelpExtra[] = {
1389 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1390 {N_("Del"), OP_DELETE},
1391 {N_("Reply"), OP_REPLY},
1392 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1397 static struct mapping_t PagerNewsHelpExtra[] = {
1398 {N_("Post"), OP_POST},
1399 {N_("Followup"), OP_FOLLOWUP},
1400 {N_("Del"), OP_DELETE},
1401 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1408 /* This pager is actually not so simple as it once was. It now operates in
1409 two modes: one for viewing messages and the other for viewing help. These
1410 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1411 is there so that we can do operations on the current message without the
1412 need to pop back out to the main-menu. */
1414 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1416 static char searchbuf[STRING];
1417 char buffer[LONG_STRING];
1418 char helpstr[SHORT_STRING * 2];
1419 char tmphelp[SHORT_STRING * 2];
1420 int maxLine, lastLine = 0;
1421 struct line_t *lineInfo;
1422 struct q_class_t *QuoteList = NULL;
1423 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1424 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1426 int redraw = REDRAW_FULL;
1428 off_t last_pos = 0, last_offset = 0;
1429 int old_smart_wrap, old_markers;
1432 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1433 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1435 int bodyoffset = 1; /* offset of first line of real text */
1436 int statusoffset = 0; /* offset for the status bar */
1437 int helpoffset = LINES - 2; /* offset for the help bar. */
1438 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1440 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1441 int indexoffset = 0; /* offset for the PI */
1442 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1443 int indicator = indexlen / 3; /* the indicator line of the PI */
1444 int old_PagerIndexLines; /* some people want to resize it
1445 * while inside the pager... */
1451 if (!(flags & M_SHOWCOLOR))
1452 flags |= M_SHOWFLAT;
1454 if ((fp = fopen (fname, "r")) == NULL) {
1455 mutt_perror (fname);
1459 if (stat (fname, &sb) != 0) {
1460 mutt_perror (fname);
1466 /* Initialize variables */
1468 if (IsHeader (extra) && !extra->hdr->read) {
1469 Context->msgnotreadyet = extra->hdr->msgno;
1470 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1473 lineInfo = mem_malloc (sizeof (struct line_t) * (maxLine = LINES));
1474 for (i = 0; i < maxLine; i++) {
1475 memset (&lineInfo[i], 0, sizeof (struct line_t));
1476 lineInfo[i].type = -1;
1477 lineInfo[i].search_cnt = -1;
1478 lineInfo[i].syntax = mem_malloc (sizeof (struct syntax_t));
1479 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1482 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1483 if (IsHeader (extra)) {
1484 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1485 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1488 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1491 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1494 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1495 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1496 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1506 if (redraw & REDRAW_FULL) {
1507 SETCOLOR (MT_COLOR_NORMAL);
1508 /* clear() doesn't optimize screen redraws */
1512 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1513 indexlen = Context->vcount + 1;
1515 indexlen = PagerIndexLines;
1517 indicator = indexlen / 3;
1519 if (option (OPTSTATUSONTOP)) {
1521 statusoffset = IsHeader (extra) ? indexlen : 0;
1522 bodyoffset = statusoffset + 1;
1523 helpoffset = LINES - 2;
1524 bodylen = helpoffset - bodyoffset;
1525 if (!option (OPTHELP))
1531 statusoffset = LINES - 2;
1532 if (!option (OPTHELP))
1534 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1535 bodylen = statusoffset - bodyoffset;
1538 if (option (OPTHELP)) {
1539 SETCOLOR (MT_COLOR_STATUS);
1540 move (helpoffset, SW);
1541 mutt_paddstr (COLS-SW, helpstr);
1542 SETCOLOR (MT_COLOR_NORMAL);
1545 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1546 if (Resize != NULL) {
1547 if ((SearchCompiled = Resize->SearchCompiled)) {
1549 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1550 SearchFlag = M_SEARCH;
1551 SearchBack = Resize->SearchBack;
1553 lines = Resize->line;
1554 redraw |= REDRAW_SIGWINCH;
1560 if (IsHeader (extra) && PagerIndexLines) {
1561 if (index == NULL) {
1562 /* only allocate the space if/when we need the index.
1563 Initialise the menu as per the main index */
1564 index = mutt_new_menu ();
1565 index->menu = MENU_MAIN;
1566 index->make_entry = index_make_entry;
1567 index->color = index_color;
1568 index->max = Context->vcount;
1569 index->current = extra->hdr->virtual;
1572 SETCOLOR (MT_COLOR_NORMAL);
1573 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1575 index->pagelen = indexlen - 1;
1577 /* some fudge to work out where abouts the indicator should go */
1578 if (index->current - indicator < 0)
1580 else if (index->max - index->current < index->pagelen - indicator)
1581 index->top = index->max - index->pagelen;
1583 index->top = index->current - indicator;
1585 menu_redraw_index (index);
1588 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1592 if (redraw & REDRAW_SIGWINCH) {
1595 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1596 has_types | SearchFlag, &QuoteList, &q_level,
1597 &force_redraw, &SearchRE) == 0) {
1598 if (!lineInfo[i].continuation && ++j == lines) {
1603 redraw |= REDRAW_SIDEBAR;
1607 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1609 move (bodyoffset, SW);
1610 curline = oldtopline = topline;
1614 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1615 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1617 (flags & M_DISPLAYFLAGS) | hideQuoted |
1618 SearchFlag, &QuoteList, &q_level, &force_redraw,
1622 move (lines + bodyoffset, SW);
1623 redraw |= REDRAW_SIDEBAR;
1625 last_offset = lineInfo[curline].offset;
1626 } while (force_redraw);
1628 SETCOLOR (MT_COLOR_TILDE);
1629 BKGDSET (MT_COLOR_TILDE);
1630 while (lines < bodylen) {
1632 if (option (OPTTILDE))
1636 move (lines + bodyoffset, SW);
1638 /* We are going to update the pager status bar, so it isn't
1639 * necessary to reset to normal color now. */
1641 redraw |= REDRAW_STATUS; /* need to update the % seen */
1644 if (redraw & REDRAW_STATUS) {
1645 /* print out the pager status bar */
1646 SETCOLOR (MT_COLOR_STATUS);
1647 BKGDSET (MT_COLOR_STATUS);
1648 CLEARLINE_WIN (statusoffset);
1649 if (IsHeader (extra)) {
1650 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1651 size_t l2 = sizeof (buffer);
1653 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1654 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1656 else if (IsMsgAttach (extra)) {
1657 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1658 size_t l2 = sizeof (buffer);
1660 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1661 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1663 move(statusoffset,SW);
1664 mutt_paddstr (COLS - 10 - SW, IsHeader (extra) ||
1665 IsMsgAttach (extra) ? buffer : banner);
1668 if (last_pos < sb.st_size - 1)
1669 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1671 addstr (topline == 0 ? "all)" : "end)");
1672 BKGDSET (MT_COLOR_NORMAL);
1673 SETCOLOR (MT_COLOR_NORMAL);
1676 if (redraw & REDRAW_SIDEBAR)
1677 sidebar_draw (MENU_PAGER);
1679 if ((redraw & REDRAW_INDEX) && index) {
1680 /* redraw the pager_index indicator, because the
1681 * flags for this message might have changed. */
1682 menu_redraw_current (index);
1683 sidebar_draw (MENU_PAGER);
1684 /* print out the index status bar */
1685 menu_status_line (buffer, sizeof (buffer), index, NONULL (Status));
1686 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), SW);
1687 SETCOLOR (MT_COLOR_STATUS);
1688 BKGDSET (MT_COLOR_STATUS);
1689 mutt_paddstr (COLS-SW, buffer);
1690 SETCOLOR (MT_COLOR_NORMAL);
1691 BKGDSET (MT_COLOR_NORMAL);
1693 /* if we're not using the index, update every time */
1695 sidebar_draw (MENU_PAGER);
1699 if (option(OPTBRAILLEFRIENDLY)) {
1700 if (brailleLine!=-1) {
1701 move(brailleLine+1, 0);
1705 move (statusoffset, COLS-1);
1708 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1709 && lineInfo[curline].offset < sb.st_size-1) {
1710 if (TopLine - topline > lines)
1719 ch = km_dokey (MENU_PAGER);
1721 mutt_clear_error ();
1728 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1729 else if (SigWinch) {
1730 mutt_resize_screen ();
1732 /* Store current position. */
1734 for (i = 0; i <= topline; i++)
1735 if (!lineInfo[i].continuation)
1738 if (flags & M_PAGER_RETWINCH) {
1739 Resize = mem_malloc (sizeof (struct resize));
1741 Resize->line = lines;
1742 Resize->SearchCompiled = SearchCompiled;
1743 Resize->SearchBack = SearchBack;
1746 rc = OP_REFORMAT_WINCH;
1749 for (i = 0; i < maxLine; i++) {
1750 lineInfo[i].offset = 0;
1751 lineInfo[i].type = -1;
1752 lineInfo[i].continuation = 0;
1753 lineInfo[i].chunks = 0;
1754 lineInfo[i].search_cnt = -1;
1755 lineInfo[i].quote = NULL;
1757 mem_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
1758 if (SearchCompiled && lineInfo[i].search)
1759 mem_free (&(lineInfo[i].search));
1765 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1770 clearok (stdscr, TRUE); /*force complete redraw */
1774 else if (ch == -1) {
1788 if (lineInfo[curline].offset < sb.st_size - 1) {
1789 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1791 else if (option (OPTPAGERSTOP)) {
1792 /* emulate "less -q" and don't go on to the next message. */
1793 mutt_error _("Bottom of message is shown.");
1796 /* end of the current message, so display the next message. */
1797 rc = OP_MAIN_NEXT_UNDELETED;
1805 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1808 mutt_error _("Top of message is shown.");
1812 if (lineInfo[curline].offset < sb.st_size - 1) {
1815 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1821 mutt_error _("Bottom of message is shown.");
1826 topline = upNLines (1, lineInfo, topline, hideQuoted);
1828 mutt_error _("Top of message is shown.");
1835 mutt_error _("Top of message is shown.");
1840 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1842 mutt_error _("Top of message is shown.");
1846 if (lineInfo[curline].offset < sb.st_size - 1) {
1847 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1849 else if (option (OPTPAGERSTOP)) {
1850 /* emulate "less -q" and don't go on to the next message. */
1851 mutt_error _("Bottom of message is shown.");
1854 /* end of the current message, so display the next message. */
1855 rc = OP_MAIN_NEXT_UNDELETED;
1860 case OP_SEARCH_NEXT:
1861 case OP_SEARCH_OPPOSITE:
1862 if (SearchCompiled) {
1864 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1865 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1866 /* searching forward */
1867 for (i = topline + 1; i < lastLine; i++) {
1868 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1869 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1876 mutt_error _("Not found.");
1879 /* searching backward */
1880 for (i = topline - 1; i >= 0; i--) {
1881 if ((!hideQuoted || (has_types &&
1882 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1883 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1890 mutt_error _("Not found.");
1893 if (lineInfo[topline].search_cnt > 0)
1894 SearchFlag = M_SEARCH;
1898 /* no previous search pattern, so fall through to search */
1901 case OP_SEARCH_REVERSE:
1902 strfcpy (buffer, searchbuf, sizeof (buffer));
1903 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1904 _("Search: ")), buffer, sizeof (buffer),
1908 if (!strcmp (buffer, searchbuf)) {
1909 if (SearchCompiled) {
1910 /* do an implicit search-next */
1911 if (ch == OP_SEARCH)
1912 ch = OP_SEARCH_NEXT;
1914 ch = OP_SEARCH_OPPOSITE;
1923 strfcpy (searchbuf, buffer, sizeof (searchbuf));
1925 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1926 if (ch == OP_SEARCH)
1928 else if (ch == OP_SEARCH_REVERSE)
1931 if (SearchCompiled) {
1932 regfree (&SearchRE);
1933 for (i = 0; i < lastLine; i++) {
1934 if (lineInfo[i].search)
1935 mem_free (&(lineInfo[i].search));
1936 lineInfo[i].search_cnt = -1;
1941 REGCOMP (&SearchRE, searchbuf,
1942 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1943 regerror (err, &SearchRE, buffer, sizeof (buffer));
1944 mutt_error ("%s", buffer);
1945 regfree (&SearchRE);
1946 for (i = 0; i < maxLine; i++) {
1948 if (lineInfo[i].search)
1949 mem_free (&(lineInfo[i].search));
1950 lineInfo[i].search_cnt = -1;
1957 /* update the search pointers */
1959 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1960 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1961 &QuoteList, &q_level,
1962 &force_redraw, &SearchRE) == 0) {
1964 redraw |= REDRAW_SIDEBAR;
1968 /* searching forward */
1969 for (i = topline; i < lastLine; i++) {
1970 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1971 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1979 /* searching backward */
1980 for (i = topline; i >= 0; i--) {
1981 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1982 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1990 if (lineInfo[topline].search_cnt == 0) {
1992 mutt_error _("Not found.");
1995 SearchFlag = M_SEARCH;
1997 redraw = REDRAW_BODY;
2000 case OP_SEARCH_TOGGLE:
2001 if (SearchCompiled) {
2002 SearchFlag ^= M_SEARCH;
2003 redraw = REDRAW_BODY;
2008 /* don't let the user enter the help-menu from the help screen! */
2011 mutt_help (MENU_PAGER);
2012 redraw = REDRAW_FULL;
2016 mutt_error _("Help is currently being shown.");
2019 case OP_PAGER_HIDE_QUOTED:
2021 hideQuoted ^= M_HIDE;
2022 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
2023 topline = upNLines (1, lineInfo, topline, hideQuoted);
2025 redraw = REDRAW_BODY;
2029 case OP_PAGER_SKIP_QUOTED:
2032 int new_topline = topline;
2034 while ((new_topline < lastLine ||
2035 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2036 new_topline, &lastLine,
2037 &maxLine, M_TYPES, &QuoteList,
2038 &q_level, &force_redraw,
2040 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
2041 redraw |= REDRAW_SIDEBAR;
2046 mutt_error _("No more quoted text.");
2051 while ((new_topline < lastLine ||
2052 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2053 new_topline, &lastLine,
2054 &maxLine, M_TYPES, &QuoteList,
2055 &q_level, &force_redraw,
2057 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2059 redraw |= REDRAW_SIDEBAR;
2063 mutt_error _("No more unquoted text after quoted text.");
2067 topline = new_topline;
2071 case OP_PAGER_BOTTOM: /* move to the end of the file */
2072 if (lineInfo[curline].offset < sb.st_size - 1) {
2074 /* make sure the types are defined to the end of file */
2075 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2076 &maxLine, has_types,
2077 &QuoteList, &q_level, &force_redraw,
2080 redraw |= REDRAW_SIDEBAR;
2082 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2085 mutt_error _("Bottom of message is shown.");
2089 clearok (stdscr, TRUE);
2090 redraw = REDRAW_FULL;
2094 km_error_key (MENU_PAGER);
2097 /* --------------------------------------------------------------------
2098 * The following are operations on the current message rather than
2099 * adjusting the view of the message.
2102 case OP_BOUNCE_MESSAGE:
2103 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2105 if (IsMsgAttach (extra))
2106 mutt_attach_bounce (extra->fp, extra->hdr,
2107 extra->idx, extra->idxlen, extra->bdy);
2109 ci_bounce_message (extra->hdr, &redraw);
2113 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2115 if (IsMsgAttach (extra))
2116 mutt_attach_resend (extra->fp, extra->hdr,
2117 extra->idx, extra->idxlen, extra->bdy);
2119 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2120 redraw = REDRAW_FULL;
2123 case OP_CHECK_TRADITIONAL:
2124 CHECK_MODE (IsHeader (extra));
2125 if (!(WithCrypto & APPLICATION_PGP))
2127 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2129 rc = OP_CHECK_TRADITIONAL;
2133 case OP_CREATE_ALIAS:
2134 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2135 if (IsMsgAttach (extra))
2136 mutt_create_alias (extra->bdy->hdr->env, NULL);
2138 mutt_create_alias (extra->hdr->env, NULL);
2139 MAYBE_REDRAW (redraw);
2142 case OP_PURGE_MESSAGE:
2144 CHECK_MODE (IsHeader (extra));
2147 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2149 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2150 mutt_set_flag (Context, extra->hdr, M_PURGED,
2151 ch != OP_PURGE_MESSAGE ? 0 : 1);
2152 if (option (OPTDELETEUNTAG))
2153 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2154 redraw = REDRAW_STATUS | REDRAW_INDEX;
2155 if (option (OPTRESOLVE)) {
2157 rc = OP_MAIN_NEXT_UNDELETED;
2161 case OP_DELETE_THREAD:
2162 case OP_DELETE_SUBTHREAD:
2163 CHECK_MODE (IsHeader (extra));
2166 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2168 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2169 ch == OP_DELETE_THREAD ? 0 : 1);
2172 if (option (OPTDELETEUNTAG))
2173 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2174 ch == OP_DELETE_THREAD ? 0 : 1);
2175 if (option (OPTRESOLVE)) {
2176 rc = OP_MAIN_NEXT_UNDELETED;
2180 if (!option (OPTRESOLVE) && PagerIndexLines)
2181 redraw = REDRAW_FULL;
2183 redraw = REDRAW_STATUS | REDRAW_INDEX;
2187 case OP_DISPLAY_ADDRESS:
2188 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2189 if (IsMsgAttach (extra))
2190 mutt_display_address (extra->bdy->hdr->env);
2192 mutt_display_address (extra->hdr->env);
2195 case OP_ENTER_COMMAND:
2196 old_smart_wrap = option (OPTWRAP);
2197 old_markers = option (OPTMARKERS);
2198 old_PagerIndexLines = PagerIndexLines;
2200 CurrentMenu = MENU_PAGER;
2201 mutt_enter_command ();
2203 if (option (OPTNEEDRESORT)) {
2204 unset_option (OPTNEEDRESORT);
2205 CHECK_MODE (IsHeader (extra));
2206 set_option (OPTNEEDRESORT);
2209 if (old_PagerIndexLines != PagerIndexLines) {
2211 mutt_menuDestroy (&index);
2215 if (option (OPTWRAP) != old_smart_wrap ||
2216 option (OPTMARKERS) != old_markers) {
2217 if (flags & M_PAGER_RETWINCH) {
2219 rc = OP_REFORMAT_WINCH;
2223 /* count the real lines above */
2225 for (i = 0; i <= topline; i++) {
2226 if (!lineInfo[i].continuation)
2230 /* we need to restart the whole thing */
2231 for (i = 0; i < maxLine; i++) {
2232 lineInfo[i].offset = 0;
2233 lineInfo[i].type = -1;
2234 lineInfo[i].continuation = 0;
2235 lineInfo[i].chunks = 0;
2236 lineInfo[i].search_cnt = -1;
2237 lineInfo[i].quote = NULL;
2239 mem_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2240 if (SearchCompiled && lineInfo[i].search)
2241 mem_free (&(lineInfo[i].search));
2244 if (SearchCompiled) {
2245 regfree (&SearchRE);
2250 /* try to keep the old position */
2253 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2254 &lastLine, &maxLine,
2255 (has_types ? M_TYPES : 0),
2256 &QuoteList, &q_level, &force_redraw,
2258 redraw |= REDRAW_SIDEBAR;
2259 if (!lineInfo[topline].continuation)
2268 if (option (OPTFORCEREDRAWPAGER))
2269 redraw = REDRAW_FULL;
2270 unset_option (OPTFORCEREDRAWINDEX);
2271 unset_option (OPTFORCEREDRAWPAGER);
2274 case OP_FLAG_MESSAGE:
2275 CHECK_MODE (IsHeader (extra));
2278 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2280 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2281 redraw = REDRAW_STATUS | REDRAW_INDEX;
2282 if (option (OPTRESOLVE)) {
2284 rc = OP_MAIN_NEXT_UNDELETED;
2289 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2290 if (IsAttach (extra))
2291 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2293 mutt_pipe_message (extra->hdr);
2294 MAYBE_REDRAW (redraw);
2298 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2299 if (IsAttach (extra))
2300 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2302 mutt_print_message (extra->hdr);
2306 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2308 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2309 redraw = REDRAW_FULL;
2314 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2316 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2317 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2318 query_quadoption (OPT_TOMODERATED,
2320 ("Posting to this group not allowed, may be moderated. Continue?"))
2323 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2324 redraw = REDRAW_FULL;
2327 case OP_FORWARD_TO_GROUP:
2328 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2330 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2331 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2332 query_quadoption (OPT_TOMODERATED,
2334 ("Posting to this group not allowed, may be moderated. Continue?"))
2337 if (IsMsgAttach (extra))
2338 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2339 extra->idxlen, extra->bdy, SENDNEWS);
2341 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2343 redraw = REDRAW_FULL;
2347 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2350 if (IsMsgAttach (extra))
2351 followup_to = extra->bdy->hdr->env->followup_to;
2353 followup_to = extra->hdr->env->followup_to;
2355 if (!followup_to || str_casecmp (followup_to, "poster") ||
2356 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2357 _("Reply by mail as poster prefers?")) != M_YES) {
2358 if (extra->ctx && extra->ctx->magic == M_NNTP
2359 && !((NNTP_DATA *) extra->ctx->data)->allowed
2360 && query_quadoption (OPT_TOMODERATED,
2362 ("Posting to this group not allowed, may be moderated. Continue?"))
2365 if (IsMsgAttach (extra))
2366 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2367 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2369 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2370 extra->ctx, extra->hdr);
2371 redraw = REDRAW_FULL;
2377 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2379 if (IsMsgAttach (extra))
2380 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2381 extra->idxlen, extra->bdy, SENDREPLY);
2383 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2384 redraw = REDRAW_FULL;
2387 case OP_RECALL_MESSAGE:
2388 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2390 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2391 redraw = REDRAW_FULL;
2394 case OP_GROUP_REPLY:
2395 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2397 if (IsMsgAttach (extra))
2398 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2399 extra->idxlen, extra->bdy,
2400 SENDREPLY | SENDGROUPREPLY);
2402 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2404 redraw = REDRAW_FULL;
2408 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2410 if (IsMsgAttach (extra))
2411 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2412 extra->idxlen, extra->bdy,
2413 SENDREPLY | SENDLISTREPLY);
2415 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2417 redraw = REDRAW_FULL;
2420 case OP_FORWARD_MESSAGE:
2421 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2423 if (IsMsgAttach (extra))
2424 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2425 extra->idxlen, extra->bdy, 0);
2427 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2428 redraw = REDRAW_FULL;
2431 case OP_DECRYPT_SAVE:
2438 if (IsAttach (extra)) {
2439 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2444 case OP_COPY_MESSAGE:
2445 case OP_DECODE_SAVE:
2446 case OP_DECODE_COPY:
2447 case OP_DECRYPT_COPY:
2448 if (!WithCrypto && ch == OP_DECRYPT_COPY) {
2452 CHECK_MODE (IsHeader (extra));
2453 if (mutt_save_message (extra->hdr,
2454 (ch == OP_DECRYPT_SAVE) ||
2455 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2456 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2457 (ch == OP_DECRYPT_SAVE)
2458 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2459 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2460 || ch == OP_DECRYPT_SAVE)) {
2461 if (option (OPTRESOLVE)) {
2463 rc = OP_MAIN_NEXT_UNDELETED;
2466 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2468 MAYBE_REDRAW (redraw);
2471 case OP_SHELL_ESCAPE:
2472 mutt_shell_escape ();
2473 MAYBE_REDRAW (redraw);
2477 CHECK_MODE (IsHeader (extra));
2478 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2480 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2481 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2482 ? NULL : Context->last_tag);
2484 redraw = REDRAW_STATUS | REDRAW_INDEX;
2485 if (option (OPTRESOLVE)) {
2492 CHECK_MODE (IsHeader (extra));
2495 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2497 if (extra->hdr->read || extra->hdr->old)
2498 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2500 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2502 Context->msgnotreadyet = -1;
2503 redraw = REDRAW_STATUS | REDRAW_INDEX;
2504 if (option (OPTRESOLVE)) {
2506 rc = OP_MAIN_NEXT_UNDELETED;
2511 CHECK_MODE (IsHeader (extra));
2514 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2516 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2517 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2518 redraw = REDRAW_STATUS | REDRAW_INDEX;
2519 if (option (OPTRESOLVE)) {
2525 case OP_UNDELETE_THREAD:
2526 case OP_UNDELETE_SUBTHREAD:
2527 CHECK_MODE (IsHeader (extra));
2530 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2532 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2533 ch == OP_UNDELETE_THREAD ? 0 : 1)
2534 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2535 ch == OP_UNDELETE_THREAD ? 0 : 1);
2538 if (option (OPTRESOLVE)) {
2539 rc = (ch == OP_DELETE_THREAD) ?
2540 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2544 if (!option (OPTRESOLVE) && PagerIndexLines)
2545 redraw = REDRAW_FULL;
2547 redraw = REDRAW_STATUS | REDRAW_INDEX;
2556 if (option (OPTFORCEBUFFYCHECK))
2559 redraw |= REDRAW_SIDEBAR;
2562 case OP_VIEW_ATTACHMENTS:
2563 if (flags & M_PAGER_ATTACHMENT) {
2565 rc = OP_ATTACH_COLLAPSE;
2568 CHECK_MODE (IsHeader (extra));
2569 mutt_view_attachments (extra->hdr);
2570 if (extra->hdr->attach_del)
2571 Context->changed = 1;
2572 redraw = REDRAW_FULL;
2577 if (!(WithCrypto & APPLICATION_PGP)) {
2581 CHECK_MODE (IsHeader (extra));
2583 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2584 redraw = REDRAW_FULL;
2588 case OP_FORGET_PASSPHRASE:
2589 crypt_forget_passphrase ();
2592 case OP_EXTRACT_KEYS:
2597 CHECK_MODE (IsHeader (extra));
2598 crypt_extract_keys_from_messages (extra->hdr);
2599 redraw = REDRAW_FULL;
2602 case OP_SIDEBAR_SCROLL_UP:
2603 case OP_SIDEBAR_SCROLL_DOWN:
2604 case OP_SIDEBAR_NEXT:
2605 case OP_SIDEBAR_NEXT_NEW:
2606 case OP_SIDEBAR_PREV:
2607 case OP_SIDEBAR_PREV_NEW:
2608 sidebar_scroll (ch, MENU_PAGER);
2617 if (IsHeader (extra)) {
2618 Context->msgnotreadyet = -1;
2623 OldHdr = extra->hdr;
2627 cleanup_quote (&QuoteList);
2629 for (i = 0; i < maxLine; i++) {
2630 mem_free (&(lineInfo[i].syntax));
2631 if (SearchCompiled && lineInfo[i].search)
2632 mem_free (&(lineInfo[i].search));
2634 if (SearchCompiled) {
2635 regfree (&SearchRE);
2638 mem_free (&lineInfo);
2640 mutt_menuDestroy (&index);
2641 return (rc != -1 ? rc : 0);