2 * Copyright (C) 1996-2002,2007 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "mutt_curses.h"
25 #include "mutt_regex.h"
27 #include "mutt_menu.h"
33 #include "mutt_crypt.h"
42 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
44 #define IsAttach(x) (x && (x)->bdy)
45 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
46 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
47 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
48 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
50 static const char *Not_available_in_this_menu = N_("Not available in this menu.");
51 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
52 static const char *Function_not_permitted_in_attach_message_mode = N_("Function not permitted in attach-message mode.");
54 /* hack to return to position when returning from index to same message */
55 static int TopLine = 0;
56 static HEADER *OldHdr = NULL;
58 #define CHECK_MODE(x) if (!(x)) \
61 mutt_error _(Not_available_in_this_menu); \
65 #define CHECK_READONLY if (Context->readonly) \
68 mutt_error _(Mailbox_is_read_only); \
72 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
75 mutt_error _(Function_not_permitted_in_attach_message_mode); \
79 #define CHECK_ACL(aclbit,action) \
80 if (!mutt_bit_isset(Context->rights,aclbit)) { \
82 mutt_error (_("Cannot %s: Operation not permitted by ACL"), action); \
92 struct q_class_t *next, *prev;
93 struct q_class_t *down, *up;
110 struct syntax_t *syntax;
111 struct syntax_t *search;
112 struct q_class_t *quote;
115 #define ANSI_OFF (1<<0)
116 #define ANSI_BLINK (1<<1)
117 #define ANSI_BOLD (1<<2)
118 #define ANSI_UNDERLINE (1<<3)
119 #define ANSI_REVERSE (1<<4)
120 #define ANSI_COLOR (1<<5)
122 typedef struct _ansi_attr {
129 static short InHelp = 0;
131 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
132 static struct resize {
139 #define NumSigLines 4
141 static int check_sig (const char *s, struct line_t *info, int n)
145 while (n > 0 && count <= NumSigLines)
147 if (info[n].type != MT_COLOR_SIGNATURE)
156 if (count > NumSigLines)
158 /* check for a blank line */
173 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags, int special,
176 int def_color; /* color without syntax hilight */
177 int color; /* final color */
178 static int last_color; /* last color set */
179 int search = 0, i, m;
182 last_color = -1; /* force attrset() */
184 if (lineInfo[n].continuation)
186 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)
206 struct q_class_t *class = lineInfo[m].quote;
210 def_color = class->color;
212 while (class && class->length > cnt)
214 def_color = class->color;
221 if (flags & M_SHOWCOLOR)
223 for (i = 0; i < lineInfo[m].chunks; i++)
225 /* we assume the chunks are sorted */
226 if (cnt > (lineInfo[m].syntax)[i].last)
228 if (cnt < (lineInfo[m].syntax)[i].first)
230 if (cnt != (lineInfo[m].syntax)[i].last)
232 color = (lineInfo[m].syntax)[i].color;
235 /* don't break here, as cnt might be
236 * in the next chunk as well */
240 if (flags & M_SEARCH)
242 for (i = 0; i < lineInfo[m].search_cnt; i++)
244 if (cnt > (lineInfo[m].search)[i].last)
246 if (cnt < (lineInfo[m].search)[i].first)
248 if (cnt != (lineInfo[m].search)[i].last)
250 color = ColorDefs[MT_COLOR_SEARCH];
257 /* handle "special" bold & underlined characters */
258 if (special || a->attr)
261 if ((a->attr & ANSI_COLOR))
264 a->pair = mutt_alloc_color (a->fg, a->bg);
266 if (a->attr & ANSI_BOLD)
271 if ((special & A_BOLD) || (a->attr & ANSI_BOLD))
273 if (ColorDefs[MT_COLOR_BOLD] && !search)
274 color = ColorDefs[MT_COLOR_BOLD];
278 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE))
280 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
281 color = ColorDefs[MT_COLOR_UNDERLINE];
283 color ^= A_UNDERLINE;
285 else if (a->attr & ANSI_REVERSE)
289 else if (a->attr & ANSI_BLINK)
293 else if (a->attr & ANSI_OFF)
299 if (color != last_color)
307 append_line (struct line_t *lineInfo, int n, int cnt)
311 lineInfo[n+1].type = lineInfo[n].type;
312 (lineInfo[n+1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
313 lineInfo[n+1].continuation = 1;
315 /* find the real start of the line */
316 for (m = n; m >= 0; m--)
317 if (lineInfo[m].continuation == 0) break;
319 (lineInfo[n+1].syntax)[0].first = m;
320 (lineInfo[n+1].syntax)[0].last = (lineInfo[n].continuation) ?
321 cnt + (lineInfo[n].syntax)[0].last : cnt;
325 new_class_color (struct q_class_t *class, int *q_level)
327 class->index = (*q_level)++;
328 class->color = ColorQuote[class->index % ColorQuoteUsed];
332 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
333 int index, int *q_level)
335 struct q_class_t * q_list;
338 new_class->index = -1;
342 if (q_list->index >= index)
345 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
348 q_list = q_list->down;
349 else if (q_list->next)
350 q_list = q_list->next;
353 while (!q_list->next)
360 q_list = q_list->next;
364 new_class->index = index;
365 new_class->color = ColorQuote[index % ColorQuoteUsed];
370 cleanup_quote (struct q_class_t **QuoteList)
372 struct q_class_t *ptr;
376 if ((*QuoteList)->down)
377 cleanup_quote (&((*QuoteList)->down));
378 ptr = (*QuoteList)->next;
379 if ((*QuoteList)->prefix)
380 FREE (&(*QuoteList)->prefix);
381 FREE (QuoteList); /* __FREE_CHECKED__ */
388 static struct q_class_t *
389 classify_quote (struct q_class_t **QuoteList, const char *qptr,
390 int length, int *force_redraw, int *q_level)
392 struct q_class_t *q_list = *QuoteList;
393 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
395 int offset, tail_lng;
398 if (ColorQuoteUsed <= 1)
400 /* not much point in classifying quotes... */
402 if (*QuoteList == NULL)
404 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
405 class->color = ColorQuote[0];
411 /* Did I mention how much I like emulating Lisp in C? */
413 /* classify quoting prefix */
416 if (length <= q_list->length)
418 /* case 1: check the top level nodes */
420 if (mutt_strncmp (qptr, q_list->prefix, length) == 0)
422 if (length == q_list->length)
423 return q_list; /* same prefix: return the current class */
425 /* found shorter prefix */
428 /* add a node above q_list */
429 tmp = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
430 tmp->prefix = (char *) safe_calloc (1, length + 1);
431 strncpy (tmp->prefix, qptr, length);
432 tmp->length = length;
434 /* replace q_list by tmp in the top level list */
437 tmp->next = q_list->next;
438 q_list->next->prev = tmp;
442 tmp->prev = q_list->prev;
443 q_list->prev->next = tmp;
446 /* make q_list a child of tmp */
450 /* q_list has no siblings for now */
454 /* update the root if necessary */
455 if (q_list == *QuoteList)
458 index = q_list->index;
460 /* tmp should be the return class too */
463 /* next class to test; if tmp is a shorter prefix for another
464 * node, that node can only be in the top level list, so don't
465 * go down after this point
471 /* found another branch for which tmp is a shorter prefix */
473 /* save the next sibling for later */
476 /* unlink q_list from the top level list */
478 q_list->next->prev = q_list->prev;
480 q_list->prev->next = q_list->next;
482 /* at this point, we have a tmp->down; link q_list to it */
484 /* sibling order is important here, q_list should be linked last */
492 index = q_list->index;
494 /* next class to test; as above, we shouldn't go down */
498 /* we found a shorter prefix, so certain quotes have changed classes */
504 /* shorter, but not a substring of the current class: try next */
505 q_list = q_list->next;
511 /* case 2: try subclassing the current top level node */
513 /* tmp != NULL means we already found a shorter prefix at case 1 */
514 if (tmp == NULL && mutt_strncmp (qptr, q_list->prefix, q_list->length) == 0)
516 /* ok, it's a subclass somewhere on this branch */
519 offset = q_list->length;
521 q_list = q_list->down;
522 tail_lng = length - offset;
523 tail_qptr = (char *) qptr + offset;
527 if (length <= q_list->length)
529 if (mutt_strncmp (tail_qptr, (q_list->prefix) + offset, tail_lng) == 0)
531 /* same prefix: return the current class */
532 if (length == q_list->length)
535 /* found shorter common prefix */
538 /* add a node above q_list */
539 tmp = (struct q_class_t *) safe_calloc (1,
540 sizeof (struct q_class_t));
541 tmp->prefix = (char *) safe_calloc (1, length + 1);
542 strncpy (tmp->prefix, qptr, length);
543 tmp->length = length;
545 /* replace q_list by tmp */
548 tmp->next = q_list->next;
549 q_list->next->prev = tmp;
553 tmp->prev = q_list->prev;
554 q_list->prev->next = tmp;
557 /* make q_list a child of tmp */
559 tmp->up = q_list->up;
561 if (tmp->up->down == q_list)
564 /* q_list has no siblings */
568 index = q_list->index;
570 /* tmp should be the return class too */
573 /* next class to test */
578 /* found another branch for which tmp is a shorter prefix */
580 /* save the next sibling for later */
583 /* unlink q_list from the top level list */
585 q_list->next->prev = q_list->prev;
587 q_list->prev->next = q_list->next;
589 /* at this point, we have a tmp->down; link q_list to it */
598 index = q_list->index;
600 /* next class to test */
604 /* we found a shorter prefix, so we need a redraw */
610 q_list = q_list->next;
616 /* longer than the current prefix: try subclassing it */
617 if (tmp == NULL && mutt_strncmp (tail_qptr, (q_list->prefix) + offset,
618 q_list->length - offset) == 0)
620 /* still a subclass: go down one level */
622 offset = q_list->length;
624 q_list = q_list->down;
625 tail_lng = length - offset;
626 tail_qptr = (char *) qptr + offset;
632 /* nope, try the next prefix */
633 q_list = q_list->next;
639 /* still not found so far: add it as a sibling to the current node */
642 tmp = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
643 tmp->prefix = (char *) safe_calloc (1, length + 1);
644 strncpy (tmp->prefix, qptr, length);
645 tmp->length = length;
649 tmp->next = ptr->down;
650 ptr->down->prev = tmp;
655 new_class_color (tmp, q_level);
662 shift_class_colors (*QuoteList, tmp, index, q_level);
669 /* nope, try the next prefix */
670 q_list = q_list->next;
678 /* not found so far: add it as a top level class */
679 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
680 class->prefix = (char *) safe_calloc (1, length + 1);
681 strncpy (class->prefix, qptr, length);
682 class->length = length;
683 new_class_color (class, q_level);
687 class->next = *QuoteList;
688 (*QuoteList)->prev = class;
694 shift_class_colors (*QuoteList, tmp, index, q_level);
699 static int brailleLine = -1;
700 static int brailleCol = -1;
702 static int check_attachment_marker (char *);
705 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
706 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
709 COLOR_LINE *color_line;
710 regmatch_t pmatch[1], smatch[1];
711 int found, offset, null_rx, i;
713 if (n == 0 || ISHEADER (lineInfo[n-1].type))
715 if (buf[0] == '\n') {
716 lineInfo[n].type = MT_COLOR_NORMAL;
717 getyx(stdscr, brailleLine, brailleCol);
718 } else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t'))
720 lineInfo[n].type = lineInfo[n-1].type; /* wrapped line */
721 (lineInfo[n].syntax)[0].color = (lineInfo[n-1].syntax)[0].color;
725 lineInfo[n].type = MT_COLOR_HDEFAULT;
726 color_line = ColorHdrList;
729 if (REGEXEC (color_line->rx, buf) == 0)
731 lineInfo[n].type = MT_COLOR_HEADER;
732 lineInfo[n].syntax[0].color = color_line->pair;
735 color_line = color_line->next;
739 else if (mutt_strncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
740 lineInfo[n].type = MT_COLOR_NORMAL;
742 else if (mutt_strncmp ("[-- ", buf, 4) == 0)
743 lineInfo[n].type = MT_COLOR_ATTACHMENT;
745 else if (check_attachment_marker ((char *) raw) == 0)
746 lineInfo[n].type = MT_COLOR_ATTACHMENT;
748 else if (mutt_strcmp ("-- \n", buf) == 0 || mutt_strcmp ("-- \r\n", buf) == 0)
752 lineInfo[n].type = MT_COLOR_SIGNATURE;
753 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
754 (lineInfo[i].type == MT_COLOR_NORMAL ||
755 lineInfo[i].type == MT_COLOR_QUOTED ||
756 lineInfo[i].type == MT_COLOR_HEADER))
759 if (lineInfo[i].chunks)
761 lineInfo[i].chunks = 0;
762 safe_realloc (&(lineInfo[n].syntax),
763 sizeof (struct syntax_t));
765 lineInfo[i++].type = MT_COLOR_SIGNATURE;
768 else if (check_sig (buf, lineInfo, n - 1) == 0)
769 lineInfo[n].type = MT_COLOR_SIGNATURE;
770 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0)
772 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0)
774 if (smatch[0].rm_so > 0)
778 /* hack to avoid making an extra copy of buf */
779 c = buf[smatch[0].rm_so];
780 buf[smatch[0].rm_so] = 0;
782 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0)
784 if (q_classify && lineInfo[n].quote == NULL)
785 lineInfo[n].quote = classify_quote (QuoteList,
786 buf + pmatch[0].rm_so,
787 pmatch[0].rm_eo - pmatch[0].rm_so,
788 force_redraw, q_level);
789 lineInfo[n].type = MT_COLOR_QUOTED;
792 lineInfo[n].type = MT_COLOR_NORMAL;
794 buf[smatch[0].rm_so] = c;
797 lineInfo[n].type = MT_COLOR_NORMAL;
801 if (q_classify && lineInfo[n].quote == NULL)
802 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
803 pmatch[0].rm_eo - pmatch[0].rm_so,
804 force_redraw, q_level);
805 lineInfo[n].type = MT_COLOR_QUOTED;
809 lineInfo[n].type = MT_COLOR_NORMAL;
812 if (lineInfo[n].type == MT_COLOR_NORMAL ||
813 lineInfo[n].type == MT_COLOR_QUOTED)
817 /* don't consider line endings part of the buffer
818 * for regex matching */
819 if ((nl = mutt_strlen (buf)) > 0 && buf[nl-1] == '\n')
824 lineInfo[n].chunks = 0;
832 color_line = ColorBodyList;
835 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
836 (offset ? REG_NOTBOL : 0)) == 0)
838 if (pmatch[0].rm_eo != pmatch[0].rm_so)
842 if (++(lineInfo[n].chunks) > 1)
843 safe_realloc (&(lineInfo[n].syntax),
844 (lineInfo[n].chunks) * sizeof (struct syntax_t));
846 i = lineInfo[n].chunks - 1;
847 pmatch[0].rm_so += offset;
848 pmatch[0].rm_eo += offset;
850 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
851 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
852 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last))
854 (lineInfo[n].syntax)[i].color = color_line->pair;
855 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
856 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
862 null_rx = 1; /* empty regexp; don't add it, but keep looking */
864 color_line = color_line->next;
868 offset++; /* avoid degenerate cases */
870 offset = (lineInfo[n].syntax)[i].last;
871 } while (found || null_rx);
877 static int is_ansi (unsigned char *buf)
879 while (*buf && (isdigit(*buf) || *buf == ';'))
881 return (*buf == 'm');
884 static int check_attachment_marker (char *p)
886 char *q = AttachmentMarker;
888 for (;*p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++)
890 return (int) (*p - *q);
893 static int grok_ansi(unsigned char *buf, int pos, ansi_attr *a)
897 while (isdigit(buf[x]) || buf[x] == ';')
900 /* Character Attributes */
901 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm')
907 mutt_free_color (a->fg, a->bg);
914 if (buf[pos] == '1' && (pos+1 == x || buf[pos+1] == ';'))
916 a->attr |= ANSI_BOLD;
919 else if (buf[pos] == '4' && (pos+1 == x || buf[pos+1] == ';'))
921 a->attr |= ANSI_UNDERLINE;
924 else if (buf[pos] == '5' && (pos+1 == x || buf[pos+1] == ';'))
926 a->attr |= ANSI_BLINK;
929 else if (buf[pos] == '7' && (pos+1 == x || buf[pos+1] == ';'))
931 a->attr |= ANSI_REVERSE;
934 else if (buf[pos] == '0' && (pos+1 == x || buf[pos+1] == ';'))
938 mutt_free_color(a->fg,a->bg);
944 else if (buf[pos] == '3' && isdigit(buf[pos+1]))
948 mutt_free_color(a->fg,a->bg);
951 a->attr |= ANSI_COLOR;
952 a->fg = buf[pos+1] - '0';
955 else if (buf[pos] == '4' && isdigit(buf[pos+1]))
959 mutt_free_color(a->fg,a->bg);
962 a->attr |= ANSI_COLOR;
963 a->bg = buf[pos+1] - '0';
968 while (pos < x && buf[pos] != ';') pos++;
977 /* trim tail of buf so that it contains complete multibyte characters */
979 trim_incomplete_mbyte(unsigned char *buf, size_t len)
984 memset (&mbstate, 0, sizeof (mbstate));
985 for (; len > 0; buf += k, len -= k)
987 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
990 else if (k == -1 || k == 0)
999 fill_buffer (FILE *f, LOFF_T *last_pos, LOFF_T offset, unsigned char **buf,
1000 unsigned char **fmt, size_t *blen, int *buf_ready)
1002 unsigned char *p, *q;
1006 if (*buf_ready == 0)
1008 if (offset != *last_pos)
1009 fseeko (f, offset, 0);
1010 if ((*buf = (unsigned char *) mutt_read_line ((char *) *buf, blen, f, &l, M_EOL)) == NULL)
1015 *last_pos = ftello (f);
1016 b_read = (int) (*last_pos - offset);
1019 safe_realloc (fmt, *blen);
1021 /* incomplete mbyte characters trigger a segfault in regex processing for
1022 * certain versions of glibc. Trim them if necessary. */
1023 if (b_read == *blen - 2)
1024 b_read -= trim_incomplete_mbyte(*buf, b_read);
1026 /* copy "buf" to "fmt", but without bold and underline controls */
1031 if (*p == '\010' && (p > *buf))
1033 if (*(p+1) == '_') /* underline */
1035 else if (*(p+1)) /* bold or overstrike */
1043 else if (*p == '\033' && *(p+1) == '[' && is_ansi (p + 2))
1045 while (*p++ != 'm') /* skip ANSI sequence */
1048 else if (*p == '\033' && *(p+1) == ']' && check_attachment_marker ((char *) p) == 0)
1050 dprint (2, (debugfile, "fill_buffer: Seen attachment marker.\n"));
1051 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
1063 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
1064 int flags, ansi_attr *pa, int cnt,
1065 int *pspace, int *pvch, int *pcol, int *pspecial)
1067 int space = -1; /* index of the last space or TAB */
1068 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
1069 int ch, vch, k, last_special = -1, special = 0, t;
1073 int wrap_cols = mutt_term_width ((flags & M_PAGER_NOWRAP) ? 0 : Wrap);
1075 /* FIXME: this should come from lineInfo */
1076 memset(&mbstate, 0, sizeof(mbstate));
1078 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k)
1080 /* Handle ANSI sequences */
1081 while (cnt-ch >= 2 && buf[ch] == '\033' && buf[ch+1] == '[' &&
1083 ch = grok_ansi (buf, ch+2, pa) + 1;
1085 while (cnt-ch >= 2 && buf[ch] == '\033' && buf[ch+1] == ']' &&
1086 check_attachment_marker ((char *) buf+ch) == 0)
1088 while (buf[ch++] != '\a')
1093 /* is anything left to do? */
1097 k = mbrtowc (&wc, (char *)buf+ch, cnt-ch, &mbstate);
1098 if (k == -2 || k == -1)
1100 dprint (1, (debugfile, "%s:%d: mbrtowc returned %d; errno = %d.\n",
1101 __FILE__, __LINE__, k, errno));
1102 if (col + 4 > wrap_cols)
1106 printw ("\\%03o", buf[ch]);
1113 if (Charset_is_utf8 && (wc == 0x200B || wc == 0xFEFF))
1115 dprint (3, (debugfile, "skip zero-width character U+%04X\n", (unsigned short)wc));
1119 /* Handle backspace */
1127 while ((wc1 = 0, mbstate1 = mbstate,
1128 k1 = k + mbrtowc (&wc1, (char *)buf+ch+k, cnt-ch-k, &mbstate1),
1129 k1 - k > 0 && wc1 == '\b') &&
1131 k2 = mbrtowc (&wc1, (char *)buf+ch+k1, cnt-ch-k1, &mbstate1),
1132 k2 > 0 && IsWPrint (wc1)))
1136 special |= (wc == '_' && special & A_UNDERLINE)
1137 ? A_UNDERLINE : A_BOLD;
1139 else if (wc == '_' || wc1 == '_')
1141 special |= A_UNDERLINE;
1142 wc = (wc1 == '_') ? wc : wc1;
1146 /* special = 0; / * overstrike: nothing to do! */
1156 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1157 special || last_special || pa->attr))
1159 resolve_color (*lineInfo, n, vch, flags, special, pa);
1160 last_special = special;
1168 if (col + t > wrap_cols)
1174 else if (wc == '\n')
1176 else if (wc == '\t')
1183 for (; col < t; col++)
1188 else if (wc < 0x20 || wc == 0x7f)
1190 if (col + 2 > wrap_cols)
1194 printw ("^%c", ('@' + wc) & 0x7f);
1196 else if (wc < 0x100)
1198 if (col + 4 > wrap_cols)
1202 printw ("\\%03o", wc);
1206 if (col + 1 > wrap_cols)
1210 addch (replacement_char ());
1216 *pspecial = special;
1222 * flags M_SHOWFLAT, show characters (used for displaying help)
1223 * M_SHOWCOLOR, show characters in color
1224 * otherwise don't show characters
1225 * M_HIDE, don't show quoted text
1226 * M_SEARCH, resolve search patterns
1227 * M_TYPES, compute line's type
1228 * M_PAGER_NSKIP, keeps leading whitespace
1229 * M_PAGER_MARKER, eventually show markers
1232 * -1 EOF was reached
1233 * 0 normal exit, line was not displayed
1234 * >0 normal exit, line was displayed
1238 display_line (FILE *f, LOFF_T *last_pos, struct line_t **lineInfo, int n,
1239 int *last, int *max, int flags, struct q_class_t **QuoteList,
1240 int *q_level, int *force_redraw, regex_t *SearchRE)
1242 unsigned char *buf = NULL, *fmt = NULL;
1244 unsigned char *buf_ptr = buf;
1245 int ch, vch, col, cnt, b_read;
1246 int buf_ready = 0, change_last = 0;
1252 ansi_attr a = {0,0,0,-1};
1253 regmatch_t pmatch[1];
1263 safe_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1264 for (ch = *last; ch < *max ; ch++)
1266 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1267 (*lineInfo)[ch].type = -1;
1268 (*lineInfo)[ch].search_cnt = -1;
1269 (*lineInfo)[ch].syntax = safe_malloc (sizeof (struct syntax_t));
1270 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last = -1;
1274 /* only do color hiliting if we are viewing a message */
1275 if (flags & (M_SHOWCOLOR | M_TYPES))
1277 if ((*lineInfo)[n].type == -1)
1279 /* determine the line class */
1280 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, &buf, &fmt, &buflen, &buf_ready) < 0)
1287 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1288 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1290 /* avoid race condition for continuation lines when scrolling up */
1291 for (m = n + 1; m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation; m++)
1292 (*lineInfo)[m].type = (*lineInfo)[n].type;
1295 /* this also prevents searching through the hidden lines */
1296 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1297 flags = 0; /* M_NOSHOW */
1300 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1301 * don't want to compute it every time M_TYPES is set, since this
1302 * would slow down the "bottom" function unacceptably. A compromise
1303 * solution is hence to call regexec() again, just to find out the
1304 * length of the quote prefix.
1306 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1307 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1309 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, &buf, &fmt, &buflen, &buf_ready) < 0)
1315 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1316 (*lineInfo)[n].quote = classify_quote (QuoteList,
1317 (char *) fmt + pmatch[0].rm_so,
1318 pmatch[0].rm_eo - pmatch[0].rm_so,
1319 force_redraw, q_level);
1322 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation && (*lineInfo)[n].search_cnt == -1)
1324 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, &buf, &fmt, &buflen, &buf_ready) < 0)
1332 (*lineInfo)[n].search_cnt = 0;
1333 while (regexec (SearchRE, (char *) fmt + offset, 1, pmatch, (offset ? REG_NOTBOL : 0)) == 0)
1335 if (++((*lineInfo)[n].search_cnt) > 1)
1336 safe_realloc (&((*lineInfo)[n].search),
1337 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1339 (*lineInfo)[n].search = safe_malloc (sizeof (struct syntax_t));
1340 pmatch[0].rm_so += offset;
1341 pmatch[0].rm_eo += offset;
1342 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first = pmatch[0].rm_so;
1343 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last = pmatch[0].rm_eo;
1345 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1346 offset++; /* avoid degenerate cases */
1348 offset = pmatch[0].rm_eo;
1354 if (!(flags & M_SHOW) && (*lineInfo)[n+1].offset > 0)
1356 /* we've already scanned this line, so just exit */
1360 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n+1].offset > 0)
1362 /* no need to try to display this line... */
1364 goto out; /* fake display */
1367 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, &buf, &fmt,
1368 &buflen, &buf_ready)) < 0)
1375 /* now chose a good place to break the line */
1376 cnt = format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col, &special);
1377 buf_ptr = buf + cnt;
1379 /* move the break point only if smart_wrap is set */
1380 if (option (OPTWRAP))
1384 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n' && buf[cnt] != '\r')
1387 /* skip trailing blanks */
1388 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1390 /* a very long word with leading spaces causes infinite wrapping */
1391 if ((!ch) && (flags & M_PAGER_NSKIP))
1392 buf_ptr = buf + cnt;
1397 buf_ptr = buf + cnt; /* a very long word... */
1399 if (!(flags & M_PAGER_NSKIP))
1400 /* skip leading blanks on the next line too */
1401 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1405 if (*buf_ptr == '\r')
1407 if (*buf_ptr == '\n')
1410 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n+1].continuation)
1411 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1412 (*lineInfo)[n+1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1414 /* if we don't need to display the line we are done */
1415 if (!(flags & M_SHOW))
1421 /* display the line */
1422 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1424 /* avoid a bug in ncurses... */
1425 #ifndef USE_SLANG_CURSES
1428 SETCOLOR (MT_COLOR_NORMAL);
1433 /* end the last color pattern (needed by S-Lang) */
1434 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1435 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1438 * Fill the blank space at the end of the line with the prevailing color.
1439 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1440 * to make sure to reset the color *after* that
1442 if (flags & M_SHOWCOLOR)
1444 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1445 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1446 def_color = ((*lineInfo)[m].syntax)[0].color;
1448 def_color = ColorDefs[ (*lineInfo)[m].type ];
1450 attrset (def_color);
1452 bkgdset (def_color | ' ');
1456 /* ncurses always wraps lines when you get to the right side of the
1457 * screen, but S-Lang seems to only wrap if the next character is *not*
1460 #ifndef USE_SLANG_CURSES
1466 * reset the color back to normal. This *must* come after the
1467 * addch('\n'), otherwise the color for this line will not be
1468 * filled to the right margin.
1470 if (flags & M_SHOWCOLOR)
1472 SETCOLOR(MT_COLOR_NORMAL);
1473 BKGDSET(MT_COLOR_NORMAL);
1476 /* build a return code */
1477 if (!(flags & M_SHOW))
1489 upNLines (int nlines, struct line_t *info, int cur, int hiding)
1491 while (cur > 0 && nlines > 0)
1494 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1501 static struct mapping_t PagerHelp[] = {
1502 { N_("Exit"), OP_EXIT },
1503 { N_("PrevPg"), OP_PREV_PAGE },
1504 { N_("NextPg"), OP_NEXT_PAGE },
1507 static struct mapping_t PagerHelpExtra[] = {
1508 { N_("View Attachm."), OP_VIEW_ATTACHMENTS },
1509 { N_("Del"), OP_DELETE },
1510 { N_("Reply"), OP_REPLY },
1511 { N_("Next"), OP_MAIN_NEXT_UNDELETED },
1517 /* This pager is actually not so simple as it once was. It now operates in
1518 two modes: one for viewing messages and the other for viewing help. These
1519 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1520 is there so that we can do operations on the current message without the
1521 need to pop back out to the main-menu. */
1523 mutt_pager (const char *banner, const char *fname, int flags, pager_t *extra)
1525 static char searchbuf[STRING] = "";
1526 char buffer[LONG_STRING];
1527 char helpstr[SHORT_STRING*2];
1528 char tmphelp[SHORT_STRING*2];
1529 int maxLine, lastLine = 0;
1530 struct line_t *lineInfo;
1531 struct q_class_t *QuoteList = NULL;
1532 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1533 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1534 int r = -1, wrapped = 0, searchctx = 0;
1535 int redraw = REDRAW_FULL;
1537 LOFF_T last_pos = 0, last_offset = 0;
1538 int old_smart_wrap, old_markers;
1541 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1542 int has_types = (IsHeader(extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1544 int bodyoffset = 1; /* offset of first line of real text */
1545 int statusoffset = 0; /* offset for the status bar */
1546 int helpoffset = LINES - 2; /* offset for the help bar. */
1547 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1549 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1550 int indexoffset = 0; /* offset for the PI */
1551 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1552 int indicator = indexlen / 3; /* the indicator line of the PI */
1553 int old_PagerIndexLines; /* some people want to resize it
1554 * while inside the pager... */
1556 if (!(flags & M_SHOWCOLOR))
1557 flags |= M_SHOWFLAT;
1559 if ((fp = fopen (fname, "r")) == NULL)
1561 mutt_perror (fname);
1565 if (stat (fname, &sb) != 0)
1567 mutt_perror (fname);
1573 /* Initialize variables */
1575 if (IsHeader (extra) && !extra->hdr->read)
1577 Context->msgnotreadyet = extra->hdr->msgno;
1578 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1581 lineInfo = safe_malloc (sizeof (struct line_t) * (maxLine = LINES));
1582 for (i = 0 ; i < maxLine ; i++)
1584 memset (&lineInfo[i], 0, sizeof (struct line_t));
1585 lineInfo[i].type = -1;
1586 lineInfo[i].search_cnt = -1;
1587 lineInfo[i].syntax = safe_malloc (sizeof (struct syntax_t));
1588 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1591 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1592 if (IsHeader (extra))
1594 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1595 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER, PagerHelpExtra);
1596 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1600 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1601 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1602 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1609 if (redraw & REDRAW_FULL)
1611 SETCOLOR (MT_COLOR_NORMAL);
1612 /* clear() doesn't optimize screen redraws */
1616 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1617 indexlen = Context->vcount + 1;
1619 indexlen = PagerIndexLines;
1621 indicator = indexlen / 3;
1623 if (option (OPTSTATUSONTOP))
1626 statusoffset = IsHeader (extra) ? indexlen : 0;
1627 bodyoffset = statusoffset + 1;
1628 helpoffset = LINES - 2;
1629 bodylen = helpoffset - bodyoffset;
1630 if (!option (OPTHELP))
1637 statusoffset = LINES - 2;
1638 if (!option (OPTHELP))
1640 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1641 bodylen = statusoffset - bodyoffset;
1644 if (option (OPTHELP))
1646 SETCOLOR (MT_COLOR_STATUS);
1647 move (helpoffset, 0);
1648 mutt_paddstr (COLS, helpstr);
1649 SETCOLOR (MT_COLOR_NORMAL);
1652 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1655 if ((SearchCompiled = Resize->SearchCompiled))
1658 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1659 SearchFlag = M_SEARCH;
1660 SearchBack = Resize->SearchBack;
1662 lines = Resize->line;
1663 redraw |= REDRAW_SIGWINCH;
1669 if (IsHeader (extra) && PagerIndexLines)
1673 /* only allocate the space if/when we need the index.
1674 Initialise the menu as per the main index */
1675 index = mutt_new_menu(MENU_MAIN);
1676 index->make_entry = index_make_entry;
1677 index->color = index_color;
1678 index->max = Context->vcount;
1679 index->current = extra->hdr->virtual;
1682 SETCOLOR (MT_COLOR_NORMAL);
1683 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1685 index->pagelen = indexlen - 1;
1687 /* some fudge to work out where abouts the indicator should go */
1688 if (index->current - indicator < 0)
1690 else if (index->max - index->current < index->pagelen - indicator)
1691 index->top = index->max - index->pagelen;
1693 index->top = index->current - indicator;
1695 menu_redraw_index(index);
1698 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1702 if (redraw & REDRAW_SIGWINCH)
1706 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1707 has_types | SearchFlag | (flags & M_PAGER_NOWRAP), &QuoteList, &q_level, &force_redraw,
1709 if (!lineInfo[i].continuation && ++j == lines)
1717 if ((redraw & REDRAW_BODY) || topline != oldtopline)
1720 move (bodyoffset, 0);
1721 curline = oldtopline = topline;
1725 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1)
1727 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1729 (flags & M_DISPLAYFLAGS) | hideQuoted | SearchFlag | (flags & M_PAGER_NOWRAP),
1730 &QuoteList, &q_level, &force_redraw, &SearchRE) > 0)
1734 last_offset = lineInfo[curline].offset;
1735 } while (force_redraw);
1737 SETCOLOR (MT_COLOR_TILDE);
1738 BKGDSET (MT_COLOR_TILDE);
1739 while (lines < bodylen)
1742 if (option (OPTTILDE))
1747 /* We are going to update the pager status bar, so it isn't
1748 * necessary to reset to normal color now. */
1750 redraw |= REDRAW_STATUS; /* need to update the % seen */
1753 if (redraw & REDRAW_STATUS)
1755 struct hdr_format_info hfi;
1756 char pager_progress_str[4];
1759 hfi.pager_progress = pager_progress_str;
1761 if (last_pos < sb.st_size - 1)
1762 snprintf(pager_progress_str, sizeof(pager_progress_str), OFF_T_FMT "%%", (100 * last_offset / sb.st_size));
1764 strfcpy(pager_progress_str, (topline == 0) ? "all" : "end", sizeof(pager_progress_str));
1766 /* print out the pager status bar */
1767 SETCOLOR (MT_COLOR_STATUS);
1768 BKGDSET (MT_COLOR_STATUS);
1769 CLEARLINE (statusoffset);
1771 if (IsHeader (extra) || IsMsgAttach (extra))
1773 size_t l1 = COLS * MB_LEN_MAX;
1774 size_t l2 = sizeof (buffer);
1775 hfi.hdr = (IsHeader (extra)) ? extra->hdr : extra->bdy->hdr;
1776 mutt_make_string_info (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt), &hfi, M_FORMAT_MAKEPRINT);
1777 mutt_paddstr (COLS, buffer);
1782 snprintf (bn, sizeof (bn), "%s (%s)", banner, pager_progress_str);
1783 mutt_paddstr (COLS, bn);
1785 BKGDSET (MT_COLOR_NORMAL);
1786 SETCOLOR (MT_COLOR_NORMAL);
1789 if ((redraw & REDRAW_INDEX) && index)
1791 /* redraw the pager_index indicator, because the
1792 * flags for this message might have changed. */
1793 menu_redraw_current (index);
1795 /* print out the index status bar */
1796 menu_status_line (buffer, sizeof (buffer), index, NONULL(Status));
1798 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), 0);
1799 SETCOLOR (MT_COLOR_STATUS);
1800 BKGDSET (MT_COLOR_STATUS);
1801 mutt_paddstr (COLS, buffer);
1802 SETCOLOR (MT_COLOR_NORMAL);
1803 BKGDSET (MT_COLOR_NORMAL);
1808 if (option(OPTBRAILLEFRIENDLY)) {
1809 if (brailleLine!=-1) {
1810 move(brailleLine+1, 0);
1813 } else move (statusoffset, COLS-1);
1816 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1817 && lineInfo[curline].offset < sb.st_size-1)
1819 if (TopLine - topline > lines)
1828 ch = km_dokey (MENU_PAGER);
1830 mutt_clear_error ();
1838 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1841 mutt_resize_screen ();
1843 /* Store current position. */
1845 for (i = 0; i <= topline; i++)
1846 if (!lineInfo[i].continuation)
1849 if (flags & M_PAGER_RETWINCH)
1851 Resize = safe_malloc (sizeof (struct resize));
1853 Resize->line = lines;
1854 Resize->SearchCompiled = SearchCompiled;
1855 Resize->SearchBack = SearchBack;
1858 rc = OP_REFORMAT_WINCH;
1862 for (i = 0; i < maxLine; i++)
1864 lineInfo[i].offset = 0;
1865 lineInfo[i].type = -1;
1866 lineInfo[i].continuation = 0;
1867 lineInfo[i].chunks = 0;
1868 lineInfo[i].search_cnt = -1;
1869 lineInfo[i].quote = NULL;
1871 safe_realloc (&(lineInfo[i].syntax),
1872 sizeof (struct syntax_t));
1873 if (SearchCompiled && lineInfo[i].search)
1874 FREE (&(lineInfo[i].search));
1880 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1885 clearok(stdscr,TRUE);/*force complete redraw*/
1905 if (lineInfo[curline].offset < sb.st_size-1)
1907 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1909 else if (option (OPTPAGERSTOP))
1911 /* emulate "less -q" and don't go on to the next message. */
1912 mutt_error _("Bottom of message is shown.");
1916 /* end of the current message, so display the next message. */
1917 rc = OP_MAIN_NEXT_UNDELETED;
1925 topline = upNLines (bodylen-PagerContext, lineInfo, topline, hideQuoted);
1928 mutt_error _("Top of message is shown.");
1932 if (lineInfo[curline].offset < sb.st_size-1)
1937 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1943 mutt_error _("Bottom of message is shown.");
1948 topline = upNLines (1, lineInfo, topline, hideQuoted);
1950 mutt_error _("Top of message is shown.");
1957 mutt_error _("Top of message is shown.");
1962 topline = upNLines (bodylen/2, lineInfo, topline, hideQuoted);
1964 mutt_error _("Top of message is shown.");
1968 if (lineInfo[curline].offset < sb.st_size-1)
1970 topline = upNLines (bodylen/2, lineInfo, curline, hideQuoted);
1972 else if (option (OPTPAGERSTOP))
1974 /* emulate "less -q" and don't go on to the next message. */
1975 mutt_error _("Bottom of message is shown.");
1979 /* end of the current message, so display the next message. */
1980 rc = OP_MAIN_NEXT_UNDELETED;
1985 case OP_SEARCH_NEXT:
1986 case OP_SEARCH_OPPOSITE:
1991 if (SearchContext > 0 && SearchContext < LINES - 2 - option (OPTHELP) ? 1 : 0)
1992 searchctx = SearchContext;
1997 if ((!SearchBack && ch==OP_SEARCH_NEXT) ||
1998 (SearchBack &&ch==OP_SEARCH_OPPOSITE))
2000 /* searching forward */
2001 for (i = wrapped ? 0 : topline + searchctx + 1; i < lastLine; i++)
2003 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2004 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2010 else if (wrapped || !option (OPTWRAPSEARCH))
2011 mutt_error _("Not found.");
2014 mutt_message _("Search wrapped to top.");
2021 /* searching backward */
2022 for (i = wrapped ? lastLine : topline + searchctx - 1; i >= 0; i--)
2024 if ((!hideQuoted || (has_types &&
2025 lineInfo[i].type != MT_COLOR_QUOTED)) &&
2026 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2032 else if (wrapped || !option (OPTWRAPSEARCH))
2033 mutt_error _("Not found.");
2036 mutt_message _("Search wrapped to bottom.");
2042 if (lineInfo[topline].search_cnt > 0)
2044 SearchFlag = M_SEARCH;
2045 /* give some context for search results */
2046 if (topline - searchctx > 0)
2047 topline -= searchctx;
2052 /* no previous search pattern, so fall through to search */
2055 case OP_SEARCH_REVERSE:
2056 strfcpy (buffer, searchbuf, sizeof (buffer));
2057 if (mutt_get_field ((ch == OP_SEARCH || ch == OP_SEARCH_NEXT) ?
2058 _("Search for: ") : _("Reverse search for: "),
2059 buffer, sizeof (buffer),
2063 if (!strcmp (buffer, searchbuf))
2067 /* do an implicit search-next */
2068 if (ch == OP_SEARCH)
2069 ch = OP_SEARCH_NEXT;
2071 ch = OP_SEARCH_OPPOSITE;
2081 strfcpy (searchbuf, buffer, sizeof (searchbuf));
2083 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
2084 if (ch == OP_SEARCH)
2086 else if (ch == OP_SEARCH_REVERSE)
2091 regfree (&SearchRE);
2092 for (i = 0; i < lastLine; i++)
2094 if (lineInfo[i].search)
2095 FREE (&(lineInfo[i].search));
2096 lineInfo[i].search_cnt = -1;
2100 if ((err = REGCOMP (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf))) != 0)
2102 regerror (err, &SearchRE, buffer, sizeof (buffer));
2103 mutt_error ("%s", buffer);
2104 regfree (&SearchRE);
2105 for (i = 0; i < maxLine ; i++)
2108 if (lineInfo[i].search)
2109 FREE (&(lineInfo[i].search));
2110 lineInfo[i].search_cnt = -1;
2118 /* update the search pointers */
2120 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2121 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP) | (flags & M_PAGER_NOWRAP),
2122 &QuoteList, &q_level,
2123 &force_redraw, &SearchRE) == 0)
2128 /* searching forward */
2129 for (i = topline; i < lastLine; i++)
2131 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2132 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2136 if (i < lastLine) topline = i;
2140 /* searching backward */
2141 for (i = topline; i >= 0; i--)
2143 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2144 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2148 if (i >= 0) topline = i;
2151 if (lineInfo[topline].search_cnt == 0)
2154 mutt_error _("Not found.");
2158 SearchFlag = M_SEARCH;
2159 /* give some context for search results */
2160 if (SearchContext > 0 && SearchContext < LINES - 2 - option (OPTHELP) ? 1 : 0)
2161 searchctx = SearchContext;
2164 if (topline - searchctx > 0)
2165 topline -= searchctx;
2169 redraw = REDRAW_BODY;
2172 case OP_SEARCH_TOGGLE:
2175 SearchFlag ^= M_SEARCH;
2176 redraw = REDRAW_BODY;
2181 /* don't let the user enter the help-menu from the help screen! */
2185 mutt_help (MENU_PAGER);
2186 redraw = REDRAW_FULL;
2190 mutt_error _("Help is currently being shown.");
2193 case OP_PAGER_HIDE_QUOTED:
2196 hideQuoted ^= M_HIDE;
2197 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
2198 topline = upNLines (1, lineInfo, topline, hideQuoted);
2200 redraw = REDRAW_BODY;
2204 case OP_PAGER_SKIP_QUOTED:
2208 int new_topline = topline;
2210 while ((new_topline < lastLine ||
2211 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2212 new_topline, &lastLine, &maxLine, M_TYPES | (flags & M_PAGER_NOWRAP),
2213 &QuoteList, &q_level, &force_redraw, &SearchRE))))
2214 && lineInfo[new_topline].type != MT_COLOR_QUOTED)
2219 mutt_error _("No more quoted text.");
2223 while ((new_topline < lastLine ||
2224 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2225 new_topline, &lastLine, &maxLine, M_TYPES | (flags & M_PAGER_NOWRAP),
2226 &QuoteList, &q_level, &force_redraw, &SearchRE))))
2227 && lineInfo[new_topline].type == MT_COLOR_QUOTED)
2232 mutt_error _("No more unquoted text after quoted text.");
2235 topline = new_topline;
2239 case OP_PAGER_BOTTOM: /* move to the end of the file */
2240 if (lineInfo[curline].offset < sb.st_size - 1)
2243 /* make sure the types are defined to the end of file */
2244 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2245 &maxLine, has_types | (flags & M_PAGER_NOWRAP),
2246 &QuoteList, &q_level, &force_redraw,
2249 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2252 mutt_error _("Bottom of message is shown.");
2256 clearok (stdscr, TRUE);
2257 redraw = REDRAW_FULL;
2261 km_error_key (MENU_PAGER);
2264 /* --------------------------------------------------------------------
2265 * The following are operations on the current message rather than
2266 * adjusting the view of the message.
2269 case OP_BOUNCE_MESSAGE:
2270 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra))
2272 if (IsMsgAttach (extra))
2273 mutt_attach_bounce (extra->fp, extra->hdr,
2274 extra->idx, extra->idxlen,
2277 ci_bounce_message (extra->hdr, &redraw);
2281 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra))
2283 if (IsMsgAttach (extra))
2284 mutt_attach_resend (extra->fp, extra->hdr,
2285 extra->idx, extra->idxlen,
2288 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2289 redraw = REDRAW_FULL;
2292 case OP_CHECK_TRADITIONAL:
2293 CHECK_MODE (IsHeader (extra));
2294 if (!(WithCrypto & APPLICATION_PGP))
2296 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED))
2299 rc = OP_CHECK_TRADITIONAL;
2303 case OP_CREATE_ALIAS:
2304 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2305 if (IsMsgAttach (extra))
2306 mutt_create_alias (extra->bdy->hdr->env, NULL);
2308 mutt_create_alias (extra->hdr->env, NULL);
2309 MAYBE_REDRAW (redraw);
2313 CHECK_MODE(IsHeader (extra));
2315 CHECK_ACL(M_ACL_DELETE, _("delete message"));
2317 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2318 if (option (OPTDELETEUNTAG))
2319 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2320 redraw = REDRAW_STATUS | REDRAW_INDEX;
2321 if (option (OPTRESOLVE))
2324 rc = OP_MAIN_NEXT_UNDELETED;
2328 case OP_MAIN_SET_FLAG:
2329 case OP_MAIN_CLEAR_FLAG:
2330 CHECK_MODE(IsHeader (extra));
2333 if (mutt_change_flag (extra->hdr, (ch == OP_MAIN_SET_FLAG)) == 0)
2334 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2335 if (extra->hdr->deleted && option (OPTRESOLVE))
2338 rc = OP_MAIN_NEXT_UNDELETED;
2342 case OP_DELETE_THREAD:
2343 case OP_DELETE_SUBTHREAD:
2344 CHECK_MODE(IsHeader (extra));
2346 CHECK_ACL(M_ACL_DELETE, _("delete message(s)"));
2348 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2349 ch == OP_DELETE_THREAD ? 0 : 1);
2353 if (option (OPTDELETEUNTAG))
2354 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2355 ch == OP_DELETE_THREAD ? 0 : 1);
2356 if (option (OPTRESOLVE))
2358 rc = OP_MAIN_NEXT_UNDELETED;
2362 if (!option (OPTRESOLVE) && PagerIndexLines)
2363 redraw = REDRAW_FULL;
2365 redraw = REDRAW_STATUS | REDRAW_INDEX;
2369 case OP_DISPLAY_ADDRESS:
2370 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2371 if (IsMsgAttach (extra))
2372 mutt_display_address (extra->bdy->hdr->env);
2374 mutt_display_address (extra->hdr->env);
2377 case OP_ENTER_COMMAND:
2378 old_smart_wrap = option (OPTWRAP);
2379 old_markers = option (OPTMARKERS);
2380 old_PagerIndexLines = PagerIndexLines;
2382 CurrentMenu = MENU_PAGER;
2383 mutt_enter_command ();
2385 if (option (OPTNEEDRESORT))
2387 unset_option (OPTNEEDRESORT);
2388 CHECK_MODE(IsHeader (extra));
2389 set_option (OPTNEEDRESORT);
2392 if (old_PagerIndexLines != PagerIndexLines)
2395 mutt_menuDestroy (&index);
2399 if (option (OPTWRAP) != old_smart_wrap ||
2400 option (OPTMARKERS) != old_markers)
2402 if (flags & M_PAGER_RETWINCH)
2405 rc = OP_REFORMAT_WINCH;
2409 /* count the real lines above */
2411 for (i = 0; i <= topline; i++)
2413 if (!lineInfo[i].continuation)
2417 /* we need to restart the whole thing */
2418 for (i = 0; i < maxLine; i++)
2420 lineInfo[i].offset = 0;
2421 lineInfo[i].type = -1;
2422 lineInfo[i].continuation = 0;
2423 lineInfo[i].chunks = 0;
2424 lineInfo[i].search_cnt = -1;
2425 lineInfo[i].quote = NULL;
2427 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2428 if (SearchCompiled && lineInfo[i].search)
2429 FREE (&(lineInfo[i].search));
2434 regfree (&SearchRE);
2439 /* try to keep the old position */
2442 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2443 &lastLine, &maxLine,
2444 (has_types ? M_TYPES : 0) | (flags & M_PAGER_NOWRAP),
2445 &QuoteList, &q_level, &force_redraw,
2448 if (! lineInfo[topline].continuation)
2457 if (option (OPTFORCEREDRAWPAGER))
2458 redraw = REDRAW_FULL;
2459 unset_option (OPTFORCEREDRAWINDEX);
2460 unset_option (OPTFORCEREDRAWPAGER);
2463 case OP_FLAG_MESSAGE:
2464 CHECK_MODE(IsHeader (extra));
2466 CHECK_ACL(M_ACL_WRITE, "flag message");
2468 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2469 redraw = REDRAW_STATUS | REDRAW_INDEX;
2470 if (option (OPTRESOLVE))
2473 rc = OP_MAIN_NEXT_UNDELETED;
2478 CHECK_MODE(IsHeader (extra) || IsAttach (extra));
2479 if (IsAttach (extra))
2480 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2482 mutt_pipe_message (extra->hdr);
2483 MAYBE_REDRAW (redraw);
2487 CHECK_MODE(IsHeader (extra) || IsAttach (extra));
2488 if (IsAttach (extra))
2489 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2491 mutt_print_message (extra->hdr);
2495 CHECK_MODE(IsHeader (extra) && !IsAttach (extra));
2497 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2498 redraw = REDRAW_FULL;
2502 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2504 if (IsMsgAttach (extra))
2505 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2506 extra->idxlen, extra->bdy,
2509 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2510 redraw = REDRAW_FULL;
2513 case OP_RECALL_MESSAGE:
2514 CHECK_MODE(IsHeader (extra) && !IsAttach(extra));
2516 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2517 redraw = REDRAW_FULL;
2520 case OP_GROUP_REPLY:
2521 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2523 if (IsMsgAttach (extra))
2524 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2525 extra->idxlen, extra->bdy, SENDREPLY|SENDGROUPREPLY);
2527 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx, extra->hdr);
2528 redraw = REDRAW_FULL;
2532 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2534 if (IsMsgAttach (extra))
2535 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2536 extra->idxlen, extra->bdy, SENDREPLY|SENDLISTREPLY);
2538 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx, extra->hdr);
2539 redraw = REDRAW_FULL;
2542 case OP_FORWARD_MESSAGE:
2543 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2545 if (IsMsgAttach (extra))
2546 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2547 extra->idxlen, extra->bdy);
2549 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2550 redraw = REDRAW_FULL;
2553 case OP_DECRYPT_SAVE:
2561 if (IsAttach (extra))
2563 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr, NULL);
2567 case OP_COPY_MESSAGE:
2568 case OP_DECODE_SAVE:
2569 case OP_DECODE_COPY:
2570 case OP_DECRYPT_COPY:
2571 if (!WithCrypto && ch == OP_DECRYPT_COPY)
2576 CHECK_MODE(IsHeader (extra));
2577 if (mutt_save_message (extra->hdr,
2578 (ch == OP_DECRYPT_SAVE) ||
2579 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2580 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2581 (ch == OP_DECRYPT_SAVE) || (ch == OP_DECRYPT_COPY) ||
2583 &redraw) == 0 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2584 || ch == OP_DECRYPT_SAVE
2587 if (option (OPTRESOLVE))
2590 rc = OP_MAIN_NEXT_UNDELETED;
2593 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2595 MAYBE_REDRAW (redraw);
2598 case OP_SHELL_ESCAPE:
2599 mutt_shell_escape ();
2600 MAYBE_REDRAW (redraw);
2604 CHECK_MODE(IsHeader (extra));
2605 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2607 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2608 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2609 ? NULL : Context->last_tag);
2611 redraw = REDRAW_STATUS | REDRAW_INDEX;
2612 if (option (OPTRESOLVE))
2620 CHECK_MODE(IsHeader (extra));
2622 CHECK_ACL(M_ACL_SEEN, _("toggle new"));
2624 if (extra->hdr->read || extra->hdr->old)
2625 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2627 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2629 Context->msgnotreadyet = -1;
2630 redraw = REDRAW_STATUS | REDRAW_INDEX;
2631 if (option (OPTRESOLVE))
2634 rc = OP_MAIN_NEXT_UNDELETED;
2639 CHECK_MODE(IsHeader (extra));
2641 CHECK_ACL(M_ACL_DELETE, _("undelete message"));
2643 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2644 redraw = REDRAW_STATUS | REDRAW_INDEX;
2645 if (option (OPTRESOLVE))
2652 case OP_UNDELETE_THREAD:
2653 case OP_UNDELETE_SUBTHREAD:
2654 CHECK_MODE(IsHeader (extra));
2656 CHECK_ACL(M_ACL_DELETE, _("undelete message(s)"));
2658 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2659 ch == OP_UNDELETE_THREAD ? 0 : 1);
2663 if (option (OPTRESOLVE))
2665 rc = (ch == OP_DELETE_THREAD) ?
2666 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2670 if (!option (OPTRESOLVE) && PagerIndexLines)
2671 redraw = REDRAW_FULL;
2673 redraw = REDRAW_STATUS | REDRAW_INDEX;
2685 case OP_VIEW_ATTACHMENTS:
2686 if (flags & M_PAGER_ATTACHMENT)
2689 rc = OP_ATTACH_COLLAPSE;
2692 CHECK_MODE(IsHeader (extra));
2693 mutt_view_attachments (extra->hdr);
2694 if (extra->hdr->attach_del)
2695 Context->changed = 1;
2696 redraw = REDRAW_FULL;
2701 if (!(WithCrypto & APPLICATION_PGP))
2706 CHECK_MODE(IsHeader(extra));
2708 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2709 redraw = REDRAW_FULL;
2713 case OP_FORGET_PASSPHRASE:
2714 crypt_forget_passphrase ();
2717 case OP_EXTRACT_KEYS:
2723 CHECK_MODE(IsHeader(extra));
2724 crypt_extract_keys_from_messages(extra->hdr);
2725 redraw = REDRAW_FULL;
2739 if (IsHeader (extra))
2741 Context->msgnotreadyet = -1;
2747 OldHdr = extra->hdr;
2751 cleanup_quote (&QuoteList);
2753 for (i = 0; i < maxLine ; i++)
2755 FREE (&(lineInfo[i].syntax));
2756 if (SearchCompiled && lineInfo[i].search)
2757 FREE (&(lineInfo[i].search));
2761 regfree (&SearchRE);
2766 mutt_menuDestroy(&index);
2767 return (rc != -1 ? rc : 0);