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"
53 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
55 #define IsAttach(x) (x && (x)->bdy)
56 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
57 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
58 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
59 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
60 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
62 static const char *Not_available_in_this_menu =
63 N_("Not available in this menu.");
64 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
65 static const char *Function_not_permitted_in_attach_message_mode =
66 N_("Function not permitted in attach-message mode.");
68 /* hack to return to position when returning from index to same message */
69 static int TopLine = 0;
70 static HEADER *OldHdr = NULL;
72 #define CHECK_MODE(x) if (!(x)) \
75 mutt_error _(Not_available_in_this_menu); \
79 #define CHECK_READONLY if (Context->readonly) \
82 mutt_error _(Mailbox_is_read_only); \
86 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
89 mutt_error _(Function_not_permitted_in_attach_message_mode); \
98 struct q_class_t *next, *prev;
99 struct q_class_t *down, *up;
114 struct syntax_t *syntax;
115 struct syntax_t *search;
116 struct q_class_t *quote;
119 #define ANSI_OFF (1<<0)
120 #define ANSI_BLINK (1<<1)
121 #define ANSI_BOLD (1<<2)
122 #define ANSI_UNDERLINE (1<<3)
123 #define ANSI_REVERSE (1<<4)
124 #define ANSI_COLOR (1<<5)
126 typedef struct _ansi_attr {
133 static short InHelp = 0;
135 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
136 static struct resize {
143 #define NumSigLines 4
145 static int check_sig (const char *s, struct line_t *info, int n)
149 while (n > 0 && count <= NumSigLines) {
150 if (info[n].type != MT_COLOR_SIGNATURE)
159 if (count > NumSigLines) {
160 /* check for a blank line */
174 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
175 int special, ansi_attr * a)
177 int def_color; /* color without syntax hilight */
178 int color; /* final color */
179 static int last_color; /* last color set */
180 int search = 0, i, m;
183 last_color = -1; /* force attrset() */
185 if (lineInfo[n].continuation) {
186 if (!cnt && option (OPTMARKERS)) {
187 SETCOLOR (MT_COLOR_MARKERS);
189 last_color = ColorDefs[MT_COLOR_MARKERS];
191 m = (lineInfo[n].syntax)[0].first;
192 cnt += (lineInfo[n].syntax)[0].last;
196 if (!(flags & M_SHOWCOLOR))
197 def_color = ColorDefs[MT_COLOR_NORMAL];
198 else if (lineInfo[m].type == MT_COLOR_HEADER)
199 def_color = (lineInfo[m].syntax)[0].color;
201 def_color = ColorDefs[lineInfo[m].type];
203 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
204 struct q_class_t *class = lineInfo[m].quote;
207 def_color = class->color;
209 while (class && class->length > cnt) {
210 def_color = class->color;
217 if (flags & M_SHOWCOLOR) {
218 for (i = 0; i < lineInfo[m].chunks; i++) {
219 /* we assume the chunks are sorted */
220 if (cnt > (lineInfo[m].syntax)[i].last)
222 if (cnt < (lineInfo[m].syntax)[i].first)
224 if (cnt != (lineInfo[m].syntax)[i].last) {
225 color = (lineInfo[m].syntax)[i].color;
228 /* don't break here, as cnt might be
229 * in the next chunk as well */
233 if (flags & M_SEARCH) {
234 for (i = 0; i < lineInfo[m].search_cnt; i++) {
235 if (cnt > (lineInfo[m].search)[i].last)
237 if (cnt < (lineInfo[m].search)[i].first)
239 if (cnt != (lineInfo[m].search)[i].last) {
240 color = ColorDefs[MT_COLOR_SEARCH];
247 /* handle "special" bold & underlined characters */
248 if (special || a->attr) {
250 if ((a->attr & ANSI_COLOR)) {
252 a->pair = mutt_alloc_color (a->fg, a->bg);
254 if (a->attr & ANSI_BOLD)
259 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
260 if (ColorDefs[MT_COLOR_BOLD] && !search)
261 color = ColorDefs[MT_COLOR_BOLD];
265 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
266 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
267 color = ColorDefs[MT_COLOR_UNDERLINE];
269 color ^= A_UNDERLINE;
271 else if (a->attr & ANSI_REVERSE) {
274 else if (a->attr & ANSI_BLINK) {
277 else if (a->attr & ANSI_OFF) {
282 if (color != last_color) {
288 static void append_line (struct line_t *lineInfo, int n, int cnt)
292 lineInfo[n + 1].type = lineInfo[n].type;
293 (lineInfo[n + 1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
294 lineInfo[n + 1].continuation = 1;
296 /* find the real start of the line */
297 for (m = n; m >= 0; m--)
298 if (lineInfo[m].continuation == 0)
301 (lineInfo[n + 1].syntax)[0].first = m;
302 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
303 cnt + (lineInfo[n].syntax)[0].last : cnt;
306 static void new_class_color (struct q_class_t *class, int *q_level)
308 class->index = (*q_level)++;
309 class->color = ColorQuote[class->index % ColorQuoteUsed];
313 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
314 int index, int *q_level)
316 struct q_class_t *q_list;
319 new_class->index = -1;
322 if (q_list->index >= index) {
324 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
327 q_list = q_list->down;
328 else if (q_list->next)
329 q_list = q_list->next;
331 while (!q_list->next) {
337 q_list = q_list->next;
341 new_class->index = index;
342 new_class->color = ColorQuote[index % ColorQuoteUsed];
346 static void cleanup_quote (struct q_class_t **QuoteList)
348 struct q_class_t *ptr;
351 if ((*QuoteList)->down)
352 cleanup_quote (&((*QuoteList)->down));
353 ptr = (*QuoteList)->next;
354 if ((*QuoteList)->prefix)
355 mem_free (&(*QuoteList)->prefix);
356 mem_free (QuoteList);
363 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
364 const char *qptr, int length,
365 int *force_redraw, int *q_level)
367 struct q_class_t *q_list = *QuoteList;
368 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
370 int offset, tail_lng;
373 if (ColorQuoteUsed <= 1) {
374 /* not much point in classifying quotes... */
376 if (*QuoteList == NULL) {
377 class = (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
378 class->color = ColorQuote[0];
384 /* Did I mention how much I like emulating Lisp in C? */
386 /* classify quoting prefix */
388 if (length <= q_list->length) {
389 /* case 1: check the top level nodes */
391 if (str_ncmp (qptr, q_list->prefix, length) == 0) {
392 if (length == q_list->length)
393 return q_list; /* same prefix: return the current class */
395 /* found shorter prefix */
397 /* add a node above q_list */
399 (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
400 tmp->prefix = (char *) mem_calloc (1, length + 1);
401 strncpy (tmp->prefix, qptr, length);
402 tmp->length = length;
404 /* replace q_list by tmp in the top level list */
406 tmp->next = q_list->next;
407 q_list->next->prev = tmp;
410 tmp->prev = q_list->prev;
411 q_list->prev->next = tmp;
414 /* make q_list a child of tmp */
418 /* q_list has no siblings for now */
422 /* update the root if necessary */
423 if (q_list == *QuoteList)
426 index = q_list->index;
428 /* tmp should be the return class too */
431 /* next class to test; if tmp is a shorter prefix for another
432 * node, that node can only be in the top level list, so don't
433 * go down after this point
438 /* found another branch for which tmp is a shorter prefix */
440 /* save the next sibling for later */
443 /* unlink q_list from the top level list */
445 q_list->next->prev = q_list->prev;
447 q_list->prev->next = q_list->next;
449 /* at this point, we have a tmp->down; link q_list to it */
451 /* sibling order is important here, q_list should be linked last */
459 index = q_list->index;
461 /* next class to test; as above, we shouldn't go down */
465 /* we found a shorter prefix, so certain quotes have changed classes */
470 /* shorter, but not a substring of the current class: try next */
471 q_list = q_list->next;
476 /* case 2: try subclassing the current top level node */
478 /* tmp != NULL means we already found a shorter prefix at case 1 */
480 && str_ncmp (qptr, q_list->prefix, q_list->length) == 0) {
481 /* ok, it's a subclass somewhere on this branch */
484 offset = q_list->length;
486 q_list = q_list->down;
487 tail_lng = length - offset;
488 tail_qptr = (char *) qptr + offset;
491 if (length <= q_list->length) {
492 if (str_ncmp (tail_qptr, (q_list->prefix) + offset, tail_lng)
494 /* same prefix: return the current class */
495 if (length == q_list->length)
498 /* found shorter common prefix */
500 /* add a node above q_list */
501 tmp = (struct q_class_t *) mem_calloc (1,
504 tmp->prefix = (char *) mem_calloc (1, length + 1);
505 strncpy (tmp->prefix, qptr, length);
506 tmp->length = length;
508 /* replace q_list by tmp */
510 tmp->next = q_list->next;
511 q_list->next->prev = tmp;
514 tmp->prev = q_list->prev;
515 q_list->prev->next = tmp;
518 /* make q_list a child of tmp */
520 tmp->up = q_list->up;
522 if (tmp->up->down == q_list)
525 /* q_list has no siblings */
529 index = q_list->index;
531 /* tmp should be the return class too */
534 /* next class to test */
538 /* found another branch for which tmp is a shorter prefix */
540 /* save the next sibling for later */
543 /* unlink q_list from the top level list */
545 q_list->next->prev = q_list->prev;
547 q_list->prev->next = q_list->next;
549 /* at this point, we have a tmp->down; link q_list to it */
558 index = q_list->index;
560 /* next class to test */
564 /* we found a shorter prefix, so we need a redraw */
569 q_list = q_list->next;
574 /* longer than the current prefix: try subclassing it */
576 && str_ncmp (tail_qptr, (q_list->prefix) + offset,
577 q_list->length - offset) == 0) {
578 /* still a subclass: go down one level */
580 offset = q_list->length;
582 q_list = q_list->down;
583 tail_lng = length - offset;
584 tail_qptr = (char *) qptr + offset;
589 /* nope, try the next prefix */
590 q_list = q_list->next;
596 /* still not found so far: add it as a sibling to the current node */
599 (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
600 tmp->prefix = (char *) mem_calloc (1, length + 1);
601 strncpy (tmp->prefix, qptr, length);
602 tmp->length = length;
605 tmp->next = ptr->down;
606 ptr->down->prev = tmp;
611 new_class_color (tmp, q_level);
617 shift_class_colors (*QuoteList, tmp, index, q_level);
623 /* nope, try the next prefix */
624 q_list = q_list->next;
631 /* not found so far: add it as a top level class */
632 class = (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
633 class->prefix = (char *) mem_calloc (1, length + 1);
634 strncpy (class->prefix, qptr, length);
635 class->length = length;
636 new_class_color (class, q_level);
639 class->next = *QuoteList;
640 (*QuoteList)->prev = class;
646 shift_class_colors (*QuoteList, tmp, index, q_level);
651 static int brailleLine = -1;
652 static int brailleCol = -1;
654 static int check_attachment_marker (char *);
657 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
658 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
661 COLOR_LINE *color_line;
662 regmatch_t pmatch[1], smatch[1];
663 int found, offset, null_rx, i;
665 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
666 if (buf[0] == '\n') {
667 lineInfo[n].type = MT_COLOR_NORMAL;
668 getyx(stdscr, brailleLine, brailleCol);
670 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
671 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
672 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
675 lineInfo[n].type = MT_COLOR_HDEFAULT;
676 color_line = ColorHdrList;
678 if (REGEXEC (&color_line->rx, buf) == 0) {
679 lineInfo[n].type = MT_COLOR_HEADER;
680 lineInfo[n].syntax[0].color = color_line->pair;
683 color_line = color_line->next;
687 else if (str_ncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
688 lineInfo[n].type = MT_COLOR_NORMAL;
690 else if (str_ncmp ("[-- ", buf, 4) == 0)
691 lineInfo[n].type = MT_COLOR_ATTACHMENT;
693 else if (check_attachment_marker ((char *) raw) == 0)
694 lineInfo[n].type = MT_COLOR_ATTACHMENT;
696 else if (str_cmp ("-- \n", buf) == 0
697 || str_cmp ("-- \r\n", buf) == 0) {
700 lineInfo[n].type = MT_COLOR_SIGNATURE;
701 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
702 (lineInfo[i].type == MT_COLOR_NORMAL ||
703 lineInfo[i].type == MT_COLOR_QUOTED ||
704 lineInfo[i].type == MT_COLOR_HEADER)) {
706 if (lineInfo[i].chunks) {
707 lineInfo[i].chunks = 0;
708 mem_realloc (&(lineInfo[n].syntax), sizeof (struct syntax_t));
710 lineInfo[i++].type = MT_COLOR_SIGNATURE;
713 else if (check_sig (buf, lineInfo, n - 1) == 0)
714 lineInfo[n].type = MT_COLOR_SIGNATURE;
715 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
716 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
717 if (smatch[0].rm_so > 0) {
720 /* hack to avoid making an extra copy of buf */
721 c = buf[smatch[0].rm_so];
722 buf[smatch[0].rm_so] = 0;
724 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
725 if (q_classify && lineInfo[n].quote == NULL)
726 lineInfo[n].quote = classify_quote (QuoteList,
727 buf + pmatch[0].rm_so,
729 pmatch[0].rm_so, force_redraw,
731 lineInfo[n].type = MT_COLOR_QUOTED;
734 lineInfo[n].type = MT_COLOR_NORMAL;
736 buf[smatch[0].rm_so] = c;
739 lineInfo[n].type = MT_COLOR_NORMAL;
742 if (q_classify && lineInfo[n].quote == NULL)
743 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
744 pmatch[0].rm_eo - pmatch[0].rm_so,
745 force_redraw, q_level);
746 lineInfo[n].type = MT_COLOR_QUOTED;
750 lineInfo[n].type = MT_COLOR_NORMAL;
753 if (lineInfo[n].type == MT_COLOR_NORMAL ||
754 lineInfo[n].type == MT_COLOR_QUOTED) {
758 lineInfo[n].chunks = 0;
765 color_line = ColorBodyList;
767 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
768 (offset ? REG_NOTBOL : 0)) == 0) {
769 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
771 if (++(lineInfo[n].chunks) > 1)
772 mem_realloc (&(lineInfo[n].syntax),
773 (lineInfo[n].chunks) *
774 sizeof (struct syntax_t));
776 i = lineInfo[n].chunks - 1;
777 pmatch[0].rm_so += offset;
778 pmatch[0].rm_eo += offset;
780 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
781 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
782 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
783 (lineInfo[n].syntax)[i].color = color_line->pair;
784 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
785 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
791 null_rx = 1; /* empty regexp; don't add it, but keep looking */
793 color_line = color_line->next;
797 offset++; /* avoid degenerate cases */
799 offset = (lineInfo[n].syntax)[i].last;
800 } while (found || null_rx);
804 static int is_ansi (unsigned char *buf)
806 while (*buf && (isdigit (*buf) || *buf == ';'))
808 return (*buf == 'm');
811 static int check_attachment_marker (char *p)
813 char *q = AttachmentMarker;
815 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
816 return (int) (*p - *q);
819 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
823 while (isdigit (buf[x]) || buf[x] == ';')
826 /* Character Attributes */
827 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
831 mutt_free_color (a->fg, a->bg);
839 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
840 a->attr |= ANSI_BOLD;
843 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
844 a->attr |= ANSI_UNDERLINE;
847 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
848 a->attr |= ANSI_BLINK;
851 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
852 a->attr |= ANSI_REVERSE;
855 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
858 mutt_free_color (a->fg, a->bg);
864 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
867 mutt_free_color (a->fg, a->bg);
870 a->attr |= ANSI_COLOR;
871 if (buf[pos + 1] != '9')
872 a->fg = buf[pos + 1] - '0';
875 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
878 mutt_free_color (a->fg, a->bg);
881 a->attr |= ANSI_COLOR;
882 if (buf[pos + 1] != '9')
883 a->bg = buf[pos + 1] - '0';
887 while (pos < x && buf[pos] != ';')
897 /* trim tail of buf so that it contains complete multibyte characters */
898 static int trim_incomplete_mbyte(unsigned char *buf, size_t len) {
902 memset (&mbstate, 0, sizeof (mbstate));
903 for (; len > 0; buf += k, len -= k) {
904 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
907 else if (k == -1 || k == 0)
916 fill_buffer (FILE * f, LOFF_T *last_pos, LOFF_T offset, unsigned char *buf,
917 unsigned char *fmt, size_t blen, int *buf_ready)
920 static int b_read = 0;
922 if (*buf_ready == 0) {
924 if (offset != *last_pos)
925 fseeko (f, offset, 0);
926 if (fgets ((char *) buf, blen - 1, f) == NULL) {
930 *last_pos = ftello (f);
931 b_read = (int) (*last_pos - offset);
934 /* incomplete mbyte characters trigger a segfault in regex processing for
935 * certain versions of glibc. Trim them if necessary. */
936 if (b_read == blen - 2)
937 b_read -= trim_incomplete_mbyte(buf, b_read);
939 /* copy "buf" to "fmt", but without bold and underline controls */
942 if (*p == '\010' && (p > buf)) {
943 if (*(p + 1) == '_') /* underline */
945 else if (*(p + 1)) { /* bold or overstrike */
946 *(fmt - 1) = *(p + 1);
952 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
953 while (*p++ != 'm') /* skip ANSI sequence */
956 else if (*p == '\033' && *(p + 1) == ']'
957 && check_attachment_marker ((char *) p) == 0) {
958 debug_print (2, ("seen attachment marker.\n"));
959 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
976 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
977 int flags, ansi_attr * pa, int cnt,
978 int *pspace, int *pvch, int *pcol, int *pspecial)
980 int space = -1; /* index of the last space or TAB */
981 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
982 int ch, vch, k, last_special = -1, special = 0, t;
986 int wrap_cols = COLS;
988 if (!(flags & (M_SHOWFLAT)))
989 wrap_cols -= WrapMargin;
995 /* FIXME: this should come from lineInfo */
996 memset (&mbstate, 0, sizeof (mbstate));
998 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
999 /* Handle ANSI sequences */
1000 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
1001 is_ansi (buf + ch + 2))
1002 ch = grok_ansi (buf, ch + 2, pa) + 1;
1004 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
1005 check_attachment_marker ((char *) buf + ch) == 0) {
1006 while (buf[ch++] != '\a')
1011 /* is anything left to do? */
1015 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
1016 if (k == -2 || k == -1) {
1017 debug_print (1, ("mbrtowc returned %d; errno = %d.\n", k, errno));
1018 if (col + 4 > wrap_cols)
1022 printw ("\\%03o", buf[ch]);
1029 /* Handle backspace */
1031 if (IsWPrint (wc)) {
1036 while ((wc1 = 0, mbstate1 = mbstate,
1038 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
1039 &mbstate1), k1 - k > 0 && wc1 == '\b')
1041 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1042 &mbstate1), k2 > 0 && IsWPrint (wc1))) {
1044 special |= (wc == '_' && special & A_UNDERLINE)
1045 ? A_UNDERLINE : A_BOLD;
1047 else if (wc == '_' || wc1 == '_') {
1048 special |= A_UNDERLINE;
1049 wc = (wc1 == '_') ? wc : wc1;
1052 /* special = 0; / * overstrike: nothing to do! */
1062 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1063 special || last_special || pa->attr)) {
1064 resolve_color (*lineInfo, n, vch, flags, special, pa);
1065 last_special = special;
1068 if (IsWPrint (wc)) {
1072 if (col + t > wrap_cols)
1078 else if (wc == '\n')
1080 else if (wc == '\t') {
1086 for (; col < t; col++)
1091 else if (wc < 0x20 || wc == 0x7f) {
1092 if (col + 2 > wrap_cols)
1096 printw ("^%c", ('@' + wc) & 0x7f);
1098 else if (wc < 0x100) {
1099 if (col + 4 > wrap_cols)
1103 printw ("\\%03o", wc);
1106 if (col + 1 > wrap_cols)
1110 addch (replacement_char ());
1116 *pspecial = special;
1122 * flags M_SHOWFLAT, show characters (used for displaying help)
1123 * M_SHOWCOLOR, show characters in color
1124 * otherwise don't show characters
1125 * M_HIDE, don't show quoted text
1126 * M_SEARCH, resolve search patterns
1127 * M_TYPES, compute line's type
1128 * M_PAGER_NSKIP, keeps leading whitespace
1129 * M_PAGER_MARKER, eventually show markers
1132 * -1 EOF was reached
1133 * 0 normal exit, line was not displayed
1134 * >0 normal exit, line was displayed
1138 display_line (FILE * f, LOFF_T *last_pos, struct line_t **lineInfo, int n,
1139 int *last, int *max, int flags, struct q_class_t **QuoteList,
1140 int *q_level, int *force_redraw, regex_t * SearchRE)
1142 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1143 unsigned char *buf_ptr = buf;
1144 int ch, vch, col, cnt, b_read;
1145 int buf_ready = 0, change_last = 0;
1150 ansi_attr a = { 0, 0, 0, -1 };
1151 regmatch_t pmatch[1];
1158 if (*last == *max) {
1159 mem_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1160 for (ch = *last; ch < *max; ch++) {
1161 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1162 (*lineInfo)[ch].type = -1;
1163 (*lineInfo)[ch].search_cnt = -1;
1164 (*lineInfo)[ch].syntax = mem_malloc (sizeof (struct syntax_t));
1165 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1170 /* only do color hiliting if we are viewing a message */
1171 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1172 if ((*lineInfo)[n].type == -1) {
1173 /* determine the line class */
1175 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1182 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1183 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1185 /* avoid race condition for continuation lines when scrolling up */
1187 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1189 (*lineInfo)[m].type = (*lineInfo)[n].type;
1192 /* this also prevents searching through the hidden lines */
1193 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1194 flags = 0; /* M_NOSHOW */
1197 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1198 * don't want to compute it every time M_TYPES is set, since this
1199 * would slow down the "bottom" function unacceptably. A compromise
1200 * solution is hence to call regexec() again, just to find out the
1201 * length of the quote prefix.
1203 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1204 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1207 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1213 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1214 (*lineInfo)[n].quote = classify_quote (QuoteList,
1215 (char *) fmt + pmatch[0].rm_so,
1216 pmatch[0].rm_eo - pmatch[0].rm_so,
1217 force_redraw, q_level);
1220 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1221 && (*lineInfo)[n].search_cnt == -1) {
1223 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1231 (*lineInfo)[n].search_cnt = 0;
1233 (SearchRE, (char *) fmt + offset, 1, pmatch,
1234 (offset ? REG_NOTBOL : 0)) == 0) {
1235 if (++((*lineInfo)[n].search_cnt) > 1)
1236 mem_realloc (&((*lineInfo)[n].search),
1237 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1239 (*lineInfo)[n].search = mem_malloc (sizeof (struct syntax_t));
1240 pmatch[0].rm_so += offset;
1241 pmatch[0].rm_eo += offset;
1242 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1244 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1247 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1248 offset++; /* avoid degenerate cases */
1250 offset = pmatch[0].rm_eo;
1256 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1257 /* we've already scanned this line, so just exit */
1260 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1261 /* no need to try to display this line... */
1262 return (1); /* fake display */
1265 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1266 sizeof (buf), &buf_ready)) < 0) {
1272 /* now chose a good place to break the line */
1274 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1276 buf_ptr = buf + cnt;
1278 /* move the break point only if smart_wrap is set */
1279 if (option (OPTWRAP)) {
1281 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1282 && buf[cnt] != '\r') {
1284 /* skip trailing blanks */
1285 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1287 /* a very long word with leading spaces causes infinite wrapping */
1288 if ((!ch) && (flags & M_PAGER_NSKIP))
1289 buf_ptr = buf + cnt;
1294 buf_ptr = buf + cnt; /* a very long word... */
1296 if (!(flags & M_PAGER_NSKIP))
1297 /* skip leading blanks on the next line too */
1298 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1302 if (*buf_ptr == '\r')
1304 if (*buf_ptr == '\n')
1307 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1308 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1309 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1311 /* if we don't need to display the line we are done */
1312 if (!(flags & M_SHOW))
1315 /* display the line */
1316 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1318 /* avoid a bug in ncurses... */
1319 #ifndef USE_SLANG_CURSES
1321 SETCOLOR (MT_COLOR_NORMAL);
1326 /* end the last color pattern (needed by S-Lang) */
1327 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1328 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1331 * Fill the blank space at the end of the line with the prevailing color.
1332 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1333 * to make sure to reset the color *after* that
1335 if (flags & M_SHOWCOLOR) {
1336 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1337 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1338 def_color = ((*lineInfo)[m].syntax)[0].color;
1340 def_color = ColorDefs[(*lineInfo)[m].type];
1342 attrset (def_color);
1344 bkgdset (def_color | ' ');
1348 /* ncurses always wraps lines when you get to the right side of the
1349 * screen, but S-Lang seems to only wrap if the next character is *not*
1352 #ifndef USE_SLANG_CURSES
1358 * reset the color back to normal. This *must* come after the
1359 * addch('\n'), otherwise the color for this line will not be
1360 * filled to the right margin.
1362 if (flags & M_SHOWCOLOR) {
1363 SETCOLOR (MT_COLOR_NORMAL);
1364 BKGDSET (MT_COLOR_NORMAL);
1367 /* build a return code */
1368 if (!(flags & M_SHOW))
1374 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1376 while (cur > 0 && nlines > 0) {
1378 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1385 static struct mapping_t PagerHelp[] = {
1386 {N_("Exit"), OP_EXIT},
1387 {N_("PrevPg"), OP_PREV_PAGE},
1388 {N_("NextPg"), OP_NEXT_PAGE},
1391 static struct mapping_t PagerHelpExtra[] = {
1392 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1393 {N_("Del"), OP_DELETE},
1394 {N_("Reply"), OP_REPLY},
1395 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1400 static struct mapping_t PagerNewsHelpExtra[] = {
1401 {N_("Post"), OP_POST},
1402 {N_("Followup"), OP_FOLLOWUP},
1403 {N_("Del"), OP_DELETE},
1404 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1411 /* This pager is actually not so simple as it once was. It now operates in
1412 two modes: one for viewing messages and the other for viewing help. These
1413 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1414 is there so that we can do operations on the current message without the
1415 need to pop back out to the main-menu. */
1417 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1419 static char searchbuf[STRING];
1420 char buffer[LONG_STRING];
1421 char helpstr[SHORT_STRING * 2];
1422 char tmphelp[SHORT_STRING * 2];
1423 int maxLine, lastLine = 0;
1424 struct line_t *lineInfo;
1425 struct q_class_t *QuoteList = NULL;
1426 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1427 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1429 int redraw = REDRAW_FULL;
1431 LOFF_T last_pos = 0, last_offset = 0;
1432 int old_smart_wrap, old_markers;
1435 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1436 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1438 int bodyoffset = 1; /* offset of first line of real text */
1439 int statusoffset = 0; /* offset for the status bar */
1440 int helpoffset = LINES - 2; /* offset for the help bar. */
1441 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1443 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1444 int indexoffset = 0; /* offset for the PI */
1445 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1446 int indicator = indexlen / 3; /* the indicator line of the PI */
1447 int old_PagerIndexLines; /* some people want to resize it
1448 * while inside the pager... */
1454 if (!(flags & M_SHOWCOLOR))
1455 flags |= M_SHOWFLAT;
1457 if ((fp = fopen (fname, "r")) == NULL) {
1458 mutt_perror (fname);
1462 if (stat (fname, &sb) != 0) {
1463 mutt_perror (fname);
1469 /* Initialize variables */
1471 if (IsHeader (extra) && !extra->hdr->read) {
1472 Context->msgnotreadyet = extra->hdr->msgno;
1473 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1476 lineInfo = mem_malloc (sizeof (struct line_t) * (maxLine = LINES));
1477 for (i = 0; i < maxLine; i++) {
1478 memset (&lineInfo[i], 0, sizeof (struct line_t));
1479 lineInfo[i].type = -1;
1480 lineInfo[i].search_cnt = -1;
1481 lineInfo[i].syntax = mem_malloc (sizeof (struct syntax_t));
1482 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1485 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1486 if (IsHeader (extra)) {
1487 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1488 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1491 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1494 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1497 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1498 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1499 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1509 if (redraw & REDRAW_FULL) {
1510 SETCOLOR (MT_COLOR_NORMAL);
1511 /* clear() doesn't optimize screen redraws */
1515 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1516 indexlen = Context->vcount + 1;
1518 indexlen = PagerIndexLines;
1520 indicator = indexlen / 3;
1522 if (option (OPTSTATUSONTOP)) {
1524 statusoffset = IsHeader (extra) ? indexlen : 0;
1525 bodyoffset = statusoffset + 1;
1526 helpoffset = LINES - 2;
1527 bodylen = helpoffset - bodyoffset;
1528 if (!option (OPTHELP))
1534 statusoffset = LINES - 2;
1535 if (!option (OPTHELP))
1537 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1538 bodylen = statusoffset - bodyoffset;
1541 if (option (OPTHELP)) {
1542 SETCOLOR (MT_COLOR_STATUS);
1543 move (helpoffset, SW);
1544 mutt_paddstr (COLS-SW, helpstr);
1545 SETCOLOR (MT_COLOR_NORMAL);
1548 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1549 if (Resize != NULL) {
1550 if ((SearchCompiled = Resize->SearchCompiled)) {
1552 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1553 SearchFlag = M_SEARCH;
1554 SearchBack = Resize->SearchBack;
1556 lines = Resize->line;
1557 redraw |= REDRAW_SIGWINCH;
1563 if (IsHeader (extra) && PagerIndexLines) {
1564 if (index == NULL) {
1565 /* only allocate the space if/when we need the index.
1566 Initialise the menu as per the main index */
1567 index = mutt_new_menu ();
1568 index->menu = MENU_MAIN;
1569 index->make_entry = index_make_entry;
1570 index->color = index_color;
1571 index->max = Context->vcount;
1572 index->current = extra->hdr->virtual;
1575 SETCOLOR (MT_COLOR_NORMAL);
1576 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1578 index->pagelen = indexlen - 1;
1580 /* some fudge to work out where abouts the indicator should go */
1581 if (index->current - indicator < 0)
1583 else if (index->max - index->current < index->pagelen - indicator)
1584 index->top = index->max - index->pagelen;
1586 index->top = index->current - indicator;
1588 menu_redraw_index (index);
1591 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1595 if (redraw & REDRAW_SIGWINCH) {
1598 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1599 has_types | SearchFlag, &QuoteList, &q_level,
1600 &force_redraw, &SearchRE) == 0) {
1601 if (!lineInfo[i].continuation && ++j == lines) {
1606 redraw |= REDRAW_SIDEBAR;
1610 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1612 move (bodyoffset, SW);
1613 curline = oldtopline = topline;
1617 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1618 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1620 (flags & M_DISPLAYFLAGS) | hideQuoted |
1621 SearchFlag, &QuoteList, &q_level, &force_redraw,
1625 move (lines + bodyoffset, SW);
1626 redraw |= REDRAW_SIDEBAR;
1628 last_offset = lineInfo[curline].offset;
1629 } while (force_redraw);
1631 SETCOLOR (MT_COLOR_TILDE);
1632 BKGDSET (MT_COLOR_TILDE);
1633 while (lines < bodylen) {
1635 if (option (OPTTILDE))
1639 move (lines + bodyoffset, SW);
1641 /* We are going to update the pager status bar, so it isn't
1642 * necessary to reset to normal color now. */
1644 redraw |= REDRAW_STATUS; /* need to update the % seen */
1647 if (redraw & REDRAW_STATUS) {
1648 /* print out the pager status bar */
1649 SETCOLOR (MT_COLOR_STATUS);
1650 BKGDSET (MT_COLOR_STATUS);
1651 CLEARLINE_WIN (statusoffset);
1652 if (IsHeader (extra)) {
1653 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1654 size_t l2 = sizeof (buffer);
1656 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1657 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1659 else if (IsMsgAttach (extra)) {
1660 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1661 size_t l2 = sizeof (buffer);
1663 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1664 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1666 move(statusoffset,SW);
1667 mutt_paddstr (COLS - 10 - SW, IsHeader (extra) ||
1668 IsMsgAttach (extra) ? buffer : banner);
1671 if (last_pos < sb.st_size - 1)
1672 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1674 addstr (topline == 0 ? "all)" : "end)");
1675 BKGDSET (MT_COLOR_NORMAL);
1676 SETCOLOR (MT_COLOR_NORMAL);
1679 if (redraw & REDRAW_SIDEBAR)
1680 sidebar_draw (MENU_PAGER);
1682 if ((redraw & REDRAW_INDEX) && index) {
1683 /* redraw the pager_index indicator, because the
1684 * flags for this message might have changed. */
1685 menu_redraw_current (index);
1686 sidebar_draw (MENU_PAGER);
1687 /* print out the index status bar */
1688 menu_status_line (buffer, sizeof (buffer), index, NONULL (Status));
1689 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), SW);
1690 SETCOLOR (MT_COLOR_STATUS);
1691 BKGDSET (MT_COLOR_STATUS);
1692 mutt_paddstr (COLS-SW, buffer);
1693 SETCOLOR (MT_COLOR_NORMAL);
1694 BKGDSET (MT_COLOR_NORMAL);
1696 /* if we're not using the index, update every time */
1698 sidebar_draw (MENU_PAGER);
1702 if (option(OPTBRAILLEFRIENDLY)) {
1703 if (brailleLine!=-1) {
1704 move(brailleLine+1, 0);
1708 move (statusoffset, COLS-1);
1711 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1712 && lineInfo[curline].offset < sb.st_size-1) {
1713 if (TopLine - topline > lines)
1722 ch = km_dokey (MENU_PAGER);
1724 mutt_clear_error ();
1731 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1732 else if (SigWinch) {
1733 mutt_resize_screen ();
1735 /* Store current position. */
1737 for (i = 0; i <= topline; i++)
1738 if (!lineInfo[i].continuation)
1741 if (flags & M_PAGER_RETWINCH) {
1742 Resize = mem_malloc (sizeof (struct resize));
1744 Resize->line = lines;
1745 Resize->SearchCompiled = SearchCompiled;
1746 Resize->SearchBack = SearchBack;
1749 rc = OP_REFORMAT_WINCH;
1752 for (i = 0; i < maxLine; i++) {
1753 lineInfo[i].offset = 0;
1754 lineInfo[i].type = -1;
1755 lineInfo[i].continuation = 0;
1756 lineInfo[i].chunks = 0;
1757 lineInfo[i].search_cnt = -1;
1758 lineInfo[i].quote = NULL;
1760 mem_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
1761 if (SearchCompiled && lineInfo[i].search)
1762 mem_free (&(lineInfo[i].search));
1768 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1773 clearok (stdscr, TRUE); /*force complete redraw */
1777 else if (ch == -1) {
1791 if (lineInfo[curline].offset < sb.st_size - 1) {
1792 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1794 else if (option (OPTPAGERSTOP)) {
1795 /* emulate "less -q" and don't go on to the next message. */
1796 mutt_error _("Bottom of message is shown.");
1799 /* end of the current message, so display the next message. */
1800 rc = OP_MAIN_NEXT_UNDELETED;
1808 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1811 mutt_error _("Top of message is shown.");
1815 if (lineInfo[curline].offset < sb.st_size - 1) {
1818 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1824 mutt_error _("Bottom of message is shown.");
1829 topline = upNLines (1, lineInfo, topline, hideQuoted);
1831 mutt_error _("Top of message is shown.");
1838 mutt_error _("Top of message is shown.");
1843 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1845 mutt_error _("Top of message is shown.");
1849 if (lineInfo[curline].offset < sb.st_size - 1) {
1850 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1852 else if (option (OPTPAGERSTOP)) {
1853 /* emulate "less -q" and don't go on to the next message. */
1854 mutt_error _("Bottom of message is shown.");
1857 /* end of the current message, so display the next message. */
1858 rc = OP_MAIN_NEXT_UNDELETED;
1863 case OP_SEARCH_NEXT:
1864 case OP_SEARCH_OPPOSITE:
1865 if (SearchCompiled) {
1867 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1868 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1869 /* searching forward */
1870 for (i = topline + 1; i < lastLine; i++) {
1871 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1872 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1879 mutt_error _("Not found.");
1882 /* searching backward */
1883 for (i = topline - 1; i >= 0; i--) {
1884 if ((!hideQuoted || (has_types &&
1885 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1886 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1893 mutt_error _("Not found.");
1896 if (lineInfo[topline].search_cnt > 0)
1897 SearchFlag = M_SEARCH;
1901 /* no previous search pattern, so fall through to search */
1904 case OP_SEARCH_REVERSE:
1905 strfcpy (buffer, searchbuf, sizeof (buffer));
1906 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1907 _("Search: ")), buffer, sizeof (buffer),
1911 if (!strcmp (buffer, searchbuf)) {
1912 if (SearchCompiled) {
1913 /* do an implicit search-next */
1914 if (ch == OP_SEARCH)
1915 ch = OP_SEARCH_NEXT;
1917 ch = OP_SEARCH_OPPOSITE;
1926 strfcpy (searchbuf, buffer, sizeof (searchbuf));
1928 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1929 if (ch == OP_SEARCH)
1931 else if (ch == OP_SEARCH_REVERSE)
1934 if (SearchCompiled) {
1935 regfree (&SearchRE);
1936 for (i = 0; i < lastLine; i++) {
1937 if (lineInfo[i].search)
1938 mem_free (&(lineInfo[i].search));
1939 lineInfo[i].search_cnt = -1;
1944 REGCOMP (&SearchRE, searchbuf,
1945 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1946 regerror (err, &SearchRE, buffer, sizeof (buffer));
1947 mutt_error ("%s", buffer);
1948 regfree (&SearchRE);
1949 for (i = 0; i < maxLine; i++) {
1951 if (lineInfo[i].search)
1952 mem_free (&(lineInfo[i].search));
1953 lineInfo[i].search_cnt = -1;
1960 /* update the search pointers */
1962 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1963 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1964 &QuoteList, &q_level,
1965 &force_redraw, &SearchRE) == 0) {
1967 redraw |= REDRAW_SIDEBAR;
1971 /* searching forward */
1972 for (i = topline; i < lastLine; i++) {
1973 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1974 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1982 /* searching backward */
1983 for (i = topline; i >= 0; i--) {
1984 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1985 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1993 if (lineInfo[topline].search_cnt == 0) {
1995 mutt_error _("Not found.");
1998 SearchFlag = M_SEARCH;
2000 redraw = REDRAW_BODY;
2003 case OP_SEARCH_TOGGLE:
2004 if (SearchCompiled) {
2005 SearchFlag ^= M_SEARCH;
2006 redraw = REDRAW_BODY;
2011 /* don't let the user enter the help-menu from the help screen! */
2014 mutt_help (MENU_PAGER);
2015 redraw = REDRAW_FULL;
2019 mutt_error _("Help is currently being shown.");
2022 case OP_PAGER_HIDE_QUOTED:
2024 hideQuoted ^= M_HIDE;
2025 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
2026 topline = upNLines (1, lineInfo, topline, hideQuoted);
2028 redraw = REDRAW_BODY;
2032 case OP_PAGER_SKIP_QUOTED:
2035 int new_topline = topline;
2037 while ((new_topline < lastLine ||
2038 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2039 new_topline, &lastLine,
2040 &maxLine, M_TYPES, &QuoteList,
2041 &q_level, &force_redraw,
2043 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
2044 redraw |= REDRAW_SIDEBAR;
2049 mutt_error _("No more quoted text.");
2054 while ((new_topline < lastLine ||
2055 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2056 new_topline, &lastLine,
2057 &maxLine, M_TYPES, &QuoteList,
2058 &q_level, &force_redraw,
2060 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2062 redraw |= REDRAW_SIDEBAR;
2066 mutt_error _("No more unquoted text after quoted text.");
2070 topline = new_topline;
2074 case OP_PAGER_BOTTOM: /* move to the end of the file */
2075 if (lineInfo[curline].offset < sb.st_size - 1) {
2077 /* make sure the types are defined to the end of file */
2078 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2079 &maxLine, has_types,
2080 &QuoteList, &q_level, &force_redraw,
2083 redraw |= REDRAW_SIDEBAR;
2085 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2088 mutt_error _("Bottom of message is shown.");
2092 clearok (stdscr, TRUE);
2093 redraw = REDRAW_FULL;
2097 km_error_key (MENU_PAGER);
2100 /* --------------------------------------------------------------------
2101 * The following are operations on the current message rather than
2102 * adjusting the view of the message.
2105 case OP_BOUNCE_MESSAGE:
2106 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2108 if (IsMsgAttach (extra))
2109 mutt_attach_bounce (extra->fp, extra->hdr,
2110 extra->idx, extra->idxlen, extra->bdy);
2112 ci_bounce_message (extra->hdr, &redraw);
2116 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2118 if (IsMsgAttach (extra))
2119 mutt_attach_resend (extra->fp, extra->hdr,
2120 extra->idx, extra->idxlen, extra->bdy);
2122 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2123 redraw = REDRAW_FULL;
2126 case OP_CHECK_TRADITIONAL:
2127 CHECK_MODE (IsHeader (extra));
2128 if (!(WithCrypto & APPLICATION_PGP))
2130 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2132 rc = OP_CHECK_TRADITIONAL;
2136 case OP_CREATE_ALIAS:
2137 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2138 if (IsMsgAttach (extra))
2139 mutt_create_alias (extra->bdy->hdr->env, NULL);
2141 mutt_create_alias (extra->hdr->env, NULL);
2142 MAYBE_REDRAW (redraw);
2145 case OP_PURGE_MESSAGE:
2147 CHECK_MODE (IsHeader (extra));
2150 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2152 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2153 mutt_set_flag (Context, extra->hdr, M_PURGED,
2154 ch != OP_PURGE_MESSAGE ? 0 : 1);
2155 if (option (OPTDELETEUNTAG))
2156 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2157 redraw = REDRAW_STATUS | REDRAW_INDEX;
2158 if (option (OPTRESOLVE)) {
2160 rc = OP_MAIN_NEXT_UNDELETED;
2164 case OP_DELETE_THREAD:
2165 case OP_DELETE_SUBTHREAD:
2166 CHECK_MODE (IsHeader (extra));
2169 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2171 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2172 ch == OP_DELETE_THREAD ? 0 : 1);
2175 if (option (OPTDELETEUNTAG))
2176 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2177 ch == OP_DELETE_THREAD ? 0 : 1);
2178 if (option (OPTRESOLVE)) {
2179 rc = OP_MAIN_NEXT_UNDELETED;
2183 if (!option (OPTRESOLVE) && PagerIndexLines)
2184 redraw = REDRAW_FULL;
2186 redraw = REDRAW_STATUS | REDRAW_INDEX;
2190 case OP_DISPLAY_ADDRESS:
2191 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2192 if (IsMsgAttach (extra))
2193 mutt_display_address (extra->bdy->hdr->env);
2195 mutt_display_address (extra->hdr->env);
2198 case OP_ENTER_COMMAND:
2199 old_smart_wrap = option (OPTWRAP);
2200 old_markers = option (OPTMARKERS);
2201 old_PagerIndexLines = PagerIndexLines;
2203 CurrentMenu = MENU_PAGER;
2204 mutt_enter_command ();
2206 if (option (OPTNEEDRESORT)) {
2207 unset_option (OPTNEEDRESORT);
2208 CHECK_MODE (IsHeader (extra));
2209 set_option (OPTNEEDRESORT);
2212 if (old_PagerIndexLines != PagerIndexLines) {
2214 mutt_menuDestroy (&index);
2218 if (option (OPTWRAP) != old_smart_wrap ||
2219 option (OPTMARKERS) != old_markers) {
2220 if (flags & M_PAGER_RETWINCH) {
2222 rc = OP_REFORMAT_WINCH;
2226 /* count the real lines above */
2228 for (i = 0; i <= topline; i++) {
2229 if (!lineInfo[i].continuation)
2233 /* we need to restart the whole thing */
2234 for (i = 0; i < maxLine; i++) {
2235 lineInfo[i].offset = 0;
2236 lineInfo[i].type = -1;
2237 lineInfo[i].continuation = 0;
2238 lineInfo[i].chunks = 0;
2239 lineInfo[i].search_cnt = -1;
2240 lineInfo[i].quote = NULL;
2242 mem_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2243 if (SearchCompiled && lineInfo[i].search)
2244 mem_free (&(lineInfo[i].search));
2247 if (SearchCompiled) {
2248 regfree (&SearchRE);
2253 /* try to keep the old position */
2256 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2257 &lastLine, &maxLine,
2258 (has_types ? M_TYPES : 0),
2259 &QuoteList, &q_level, &force_redraw,
2261 redraw |= REDRAW_SIDEBAR;
2262 if (!lineInfo[topline].continuation)
2271 if (option (OPTFORCEREDRAWPAGER))
2272 redraw = REDRAW_FULL;
2273 unset_option (OPTFORCEREDRAWINDEX);
2274 unset_option (OPTFORCEREDRAWPAGER);
2277 case OP_FLAG_MESSAGE:
2278 CHECK_MODE (IsHeader (extra));
2281 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2283 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2284 redraw = REDRAW_STATUS | REDRAW_INDEX;
2285 if (option (OPTRESOLVE)) {
2287 rc = OP_MAIN_NEXT_UNDELETED;
2292 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2293 if (IsAttach (extra))
2294 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2296 mutt_pipe_message (extra->hdr);
2297 MAYBE_REDRAW (redraw);
2301 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2302 if (IsAttach (extra))
2303 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2305 mutt_print_message (extra->hdr);
2309 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2311 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2312 redraw = REDRAW_FULL;
2317 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2319 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2320 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2321 query_quadoption (OPT_TOMODERATED,
2323 ("Posting to this group not allowed, may be moderated. Continue?"))
2326 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2327 redraw = REDRAW_FULL;
2330 case OP_FORWARD_TO_GROUP:
2331 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2333 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2334 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2335 query_quadoption (OPT_TOMODERATED,
2337 ("Posting to this group not allowed, may be moderated. Continue?"))
2340 if (IsMsgAttach (extra))
2341 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2342 extra->idxlen, extra->bdy, SENDNEWS);
2344 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2346 redraw = REDRAW_FULL;
2350 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2353 if (IsMsgAttach (extra))
2354 followup_to = extra->bdy->hdr->env->followup_to;
2356 followup_to = extra->hdr->env->followup_to;
2358 if (!followup_to || str_casecmp (followup_to, "poster") ||
2359 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2360 _("Reply by mail as poster prefers?")) != M_YES) {
2361 if (extra->ctx && extra->ctx->magic == M_NNTP
2362 && !((NNTP_DATA *) extra->ctx->data)->allowed
2363 && query_quadoption (OPT_TOMODERATED,
2365 ("Posting to this group not allowed, may be moderated. Continue?"))
2368 if (IsMsgAttach (extra))
2369 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2370 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2372 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2373 extra->ctx, extra->hdr);
2374 redraw = REDRAW_FULL;
2380 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2382 if (IsMsgAttach (extra))
2383 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2384 extra->idxlen, extra->bdy, SENDREPLY);
2386 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2387 redraw = REDRAW_FULL;
2390 case OP_RECALL_MESSAGE:
2391 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2393 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2394 redraw = REDRAW_FULL;
2397 case OP_GROUP_REPLY:
2398 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2400 if (IsMsgAttach (extra))
2401 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2402 extra->idxlen, extra->bdy,
2403 SENDREPLY | SENDGROUPREPLY);
2405 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2407 redraw = REDRAW_FULL;
2411 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2413 if (IsMsgAttach (extra))
2414 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2415 extra->idxlen, extra->bdy,
2416 SENDREPLY | SENDLISTREPLY);
2418 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2420 redraw = REDRAW_FULL;
2423 case OP_FORWARD_MESSAGE:
2424 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2426 if (IsMsgAttach (extra))
2427 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2428 extra->idxlen, extra->bdy, 0);
2430 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2431 redraw = REDRAW_FULL;
2434 case OP_DECRYPT_SAVE:
2441 if (IsAttach (extra)) {
2442 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2447 case OP_COPY_MESSAGE:
2448 case OP_DECODE_SAVE:
2449 case OP_DECODE_COPY:
2450 case OP_DECRYPT_COPY:
2451 if (!WithCrypto && ch == OP_DECRYPT_COPY) {
2455 CHECK_MODE (IsHeader (extra));
2456 if (mutt_save_message (extra->hdr,
2457 (ch == OP_DECRYPT_SAVE) ||
2458 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2459 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2460 (ch == OP_DECRYPT_SAVE)
2461 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2462 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2463 || ch == OP_DECRYPT_SAVE)) {
2464 if (option (OPTRESOLVE)) {
2466 rc = OP_MAIN_NEXT_UNDELETED;
2469 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2471 MAYBE_REDRAW (redraw);
2474 case OP_SHELL_ESCAPE:
2475 mutt_shell_escape ();
2476 MAYBE_REDRAW (redraw);
2480 CHECK_MODE (IsHeader (extra));
2481 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2483 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2484 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2485 ? NULL : Context->last_tag);
2487 redraw = REDRAW_STATUS | REDRAW_INDEX;
2488 if (option (OPTRESOLVE)) {
2495 CHECK_MODE (IsHeader (extra));
2498 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2500 if (extra->hdr->read || extra->hdr->old)
2501 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2503 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2505 Context->msgnotreadyet = -1;
2506 redraw = REDRAW_STATUS | REDRAW_INDEX;
2507 if (option (OPTRESOLVE)) {
2509 rc = OP_MAIN_NEXT_UNDELETED;
2514 CHECK_MODE (IsHeader (extra));
2517 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2519 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2520 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2521 redraw = REDRAW_STATUS | REDRAW_INDEX;
2522 if (option (OPTRESOLVE)) {
2528 case OP_UNDELETE_THREAD:
2529 case OP_UNDELETE_SUBTHREAD:
2530 CHECK_MODE (IsHeader (extra));
2533 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2535 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2536 ch == OP_UNDELETE_THREAD ? 0 : 1)
2537 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2538 ch == OP_UNDELETE_THREAD ? 0 : 1);
2541 if (option (OPTRESOLVE)) {
2542 rc = (ch == OP_DELETE_THREAD) ?
2543 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2547 if (!option (OPTRESOLVE) && PagerIndexLines)
2548 redraw = REDRAW_FULL;
2550 redraw = REDRAW_STATUS | REDRAW_INDEX;
2559 if (option (OPTFORCEBUFFYCHECK))
2562 redraw |= REDRAW_SIDEBAR;
2565 case OP_VIEW_ATTACHMENTS:
2566 if (flags & M_PAGER_ATTACHMENT) {
2568 rc = OP_ATTACH_COLLAPSE;
2571 CHECK_MODE (IsHeader (extra));
2572 mutt_view_attachments (extra->hdr);
2573 if (extra->hdr->attach_del)
2574 Context->changed = 1;
2575 redraw = REDRAW_FULL;
2580 if (!(WithCrypto & APPLICATION_PGP)) {
2584 CHECK_MODE (IsHeader (extra));
2586 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2587 redraw = REDRAW_FULL;
2591 case OP_FORGET_PASSPHRASE:
2592 crypt_forget_passphrase ();
2595 case OP_EXTRACT_KEYS:
2600 CHECK_MODE (IsHeader (extra));
2601 crypt_extract_keys_from_messages (extra->hdr);
2602 redraw = REDRAW_FULL;
2605 case OP_SIDEBAR_SCROLL_UP:
2606 case OP_SIDEBAR_SCROLL_DOWN:
2607 case OP_SIDEBAR_NEXT:
2608 case OP_SIDEBAR_NEXT_NEW:
2609 case OP_SIDEBAR_PREV:
2610 case OP_SIDEBAR_PREV_NEW:
2611 sidebar_scroll (ch, MENU_PAGER);
2620 if (IsHeader (extra)) {
2621 Context->msgnotreadyet = -1;
2626 OldHdr = extra->hdr;
2630 cleanup_quote (&QuoteList);
2632 for (i = 0; i < maxLine; i++) {
2633 mem_free (&(lineInfo[i].syntax));
2634 if (SearchCompiled && lineInfo[i].search)
2635 mem_free (&(lineInfo[i].search));
2637 if (SearchCompiled) {
2638 regfree (&SearchRE);
2641 mem_free (&lineInfo);
2643 mutt_menuDestroy (&index);
2644 return (rc != -1 ? rc : 0);