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.
18 #include <lib-lib/mem.h>
22 #include "mutt_curses.h"
24 #include "mutt_menu.h"
29 #include "recvattach.h"
37 #include "imap_private.h"
40 #include "mutt_crypt.h"
46 #include "lib/debug.h"
56 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
58 #define IsAttach(x) (x && (x)->bdy)
59 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
60 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
61 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
62 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
63 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
65 static const char *Not_available_in_this_menu =
66 N_("Not available in this menu.");
67 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
68 static const char *Function_not_permitted_in_attach_message_mode =
69 N_("Function not permitted in attach-message mode.");
71 /* hack to return to position when returning from index to same message */
72 static int TopLine = 0;
73 static HEADER *OldHdr = NULL;
75 #define CHECK_MODE(x) if (!(x)) \
78 mutt_error _(Not_available_in_this_menu); \
82 #define CHECK_READONLY if (Context->readonly) \
85 mutt_error _(Mailbox_is_read_only); \
89 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
92 mutt_error _(Function_not_permitted_in_attach_message_mode); \
101 struct q_class_t *next, *prev;
102 struct q_class_t *down, *up;
117 struct syntax_t *syntax;
118 struct syntax_t *search;
119 struct q_class_t *quote;
122 #define ANSI_OFF (1<<0)
123 #define ANSI_BLINK (1<<1)
124 #define ANSI_BOLD (1<<2)
125 #define ANSI_UNDERLINE (1<<3)
126 #define ANSI_REVERSE (1<<4)
127 #define ANSI_COLOR (1<<5)
129 typedef struct _ansi_attr {
136 static short InHelp = 0;
138 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
139 static struct resize {
146 #define NumSigLines 4
148 static int check_sig (const char *s, struct line_t *info, int n)
152 while (n > 0 && count <= NumSigLines) {
153 if (info[n].type != MT_COLOR_SIGNATURE)
162 if (count > NumSigLines) {
163 /* check for a blank line */
177 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
178 int special, ansi_attr * a)
180 int def_color; /* color without syntax hilight */
181 int color; /* final color */
182 static int last_color; /* last color set */
183 int search = 0, i, m;
186 last_color = -1; /* force attrset() */
188 if (lineInfo[n].continuation) {
189 if (!cnt && option (OPTMARKERS)) {
190 SETCOLOR (MT_COLOR_MARKERS);
192 last_color = ColorDefs[MT_COLOR_MARKERS];
194 m = (lineInfo[n].syntax)[0].first;
195 cnt += (lineInfo[n].syntax)[0].last;
199 if (!(flags & M_SHOWCOLOR))
200 def_color = ColorDefs[MT_COLOR_NORMAL];
201 else if (lineInfo[m].type == MT_COLOR_HEADER)
202 def_color = (lineInfo[m].syntax)[0].color;
204 def_color = ColorDefs[lineInfo[m].type];
206 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
207 struct q_class_t *class = lineInfo[m].quote;
210 def_color = class->color;
212 while (class && class->length > cnt) {
213 def_color = class->color;
220 if (flags & M_SHOWCOLOR) {
221 for (i = 0; i < lineInfo[m].chunks; i++) {
222 /* we assume the chunks are sorted */
223 if (cnt > (lineInfo[m].syntax)[i].last)
225 if (cnt < (lineInfo[m].syntax)[i].first)
227 if (cnt != (lineInfo[m].syntax)[i].last) {
228 color = (lineInfo[m].syntax)[i].color;
231 /* don't break here, as cnt might be
232 * in the next chunk as well */
236 if (flags & M_SEARCH) {
237 for (i = 0; i < lineInfo[m].search_cnt; i++) {
238 if (cnt > (lineInfo[m].search)[i].last)
240 if (cnt < (lineInfo[m].search)[i].first)
242 if (cnt != (lineInfo[m].search)[i].last) {
243 color = ColorDefs[MT_COLOR_SEARCH];
250 /* handle "special" bold & underlined characters */
251 if (special || a->attr) {
253 if ((a->attr & ANSI_COLOR)) {
255 a->pair = mutt_alloc_color (a->fg, a->bg);
257 if (a->attr & ANSI_BOLD)
262 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
263 if (ColorDefs[MT_COLOR_BOLD] && !search)
264 color = ColorDefs[MT_COLOR_BOLD];
268 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
269 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
270 color = ColorDefs[MT_COLOR_UNDERLINE];
272 color ^= A_UNDERLINE;
274 else if (a->attr & ANSI_REVERSE) {
277 else if (a->attr & ANSI_BLINK) {
280 else if (a->attr & ANSI_OFF) {
285 if (color != last_color) {
291 static void append_line (struct line_t *lineInfo, int n, int cnt)
295 lineInfo[n + 1].type = lineInfo[n].type;
296 (lineInfo[n + 1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
297 lineInfo[n + 1].continuation = 1;
299 /* find the real start of the line */
300 for (m = n; m >= 0; m--)
301 if (lineInfo[m].continuation == 0)
304 (lineInfo[n + 1].syntax)[0].first = m;
305 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
306 cnt + (lineInfo[n].syntax)[0].last : cnt;
309 static void new_class_color (struct q_class_t *class, int *q_level)
311 class->index = (*q_level)++;
312 class->color = ColorQuote[class->index % ColorQuoteUsed];
316 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
317 int index, int *q_level)
319 struct q_class_t *q_list;
322 new_class->index = -1;
325 if (q_list->index >= index) {
327 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
330 q_list = q_list->down;
331 else if (q_list->next)
332 q_list = q_list->next;
334 while (!q_list->next) {
340 q_list = q_list->next;
344 new_class->index = index;
345 new_class->color = ColorQuote[index % ColorQuoteUsed];
349 static void cleanup_quote (struct q_class_t **QuoteList)
351 struct q_class_t *ptr;
354 if ((*QuoteList)->down)
355 cleanup_quote (&((*QuoteList)->down));
356 ptr = (*QuoteList)->next;
357 if ((*QuoteList)->prefix)
358 p_delete(&(*QuoteList)->prefix);
366 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
367 const char *qptr, int length,
368 int *force_redraw, int *q_level)
370 struct q_class_t *q_list = *QuoteList;
371 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
373 int offset, tail_lng;
376 if (ColorQuoteUsed <= 1) {
377 /* not much point in classifying quotes... */
379 if (*QuoteList == NULL) {
380 class = (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
381 class->color = ColorQuote[0];
387 /* Did I mention how much I like emulating Lisp in C? */
389 /* classify quoting prefix */
391 if (length <= q_list->length) {
392 /* case 1: check the top level nodes */
394 if (str_ncmp (qptr, q_list->prefix, length) == 0) {
395 if (length == q_list->length)
396 return q_list; /* same prefix: return the current class */
398 /* found shorter prefix */
400 /* add a node above q_list */
402 (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
403 tmp->prefix = (char *) mem_calloc (1, length + 1);
404 strncpy (tmp->prefix, qptr, length);
405 tmp->length = length;
407 /* replace q_list by tmp in the top level list */
409 tmp->next = q_list->next;
410 q_list->next->prev = tmp;
413 tmp->prev = q_list->prev;
414 q_list->prev->next = tmp;
417 /* make q_list a child of tmp */
421 /* q_list has no siblings for now */
425 /* update the root if necessary */
426 if (q_list == *QuoteList)
429 index = q_list->index;
431 /* tmp should be the return class too */
434 /* next class to test; if tmp is a shorter prefix for another
435 * node, that node can only be in the top level list, so don't
436 * go down after this point
441 /* found another branch for which tmp is a shorter prefix */
443 /* save the next sibling for later */
446 /* unlink q_list from the top level list */
448 q_list->next->prev = q_list->prev;
450 q_list->prev->next = q_list->next;
452 /* at this point, we have a tmp->down; link q_list to it */
454 /* sibling order is important here, q_list should be linked last */
462 index = q_list->index;
464 /* next class to test; as above, we shouldn't go down */
468 /* we found a shorter prefix, so certain quotes have changed classes */
473 /* shorter, but not a substring of the current class: try next */
474 q_list = q_list->next;
479 /* case 2: try subclassing the current top level node */
481 /* tmp != NULL means we already found a shorter prefix at case 1 */
483 && str_ncmp (qptr, q_list->prefix, q_list->length) == 0) {
484 /* ok, it's a subclass somewhere on this branch */
487 offset = q_list->length;
489 q_list = q_list->down;
490 tail_lng = length - offset;
491 tail_qptr = (char *) qptr + offset;
494 if (length <= q_list->length) {
495 if (str_ncmp (tail_qptr, (q_list->prefix) + offset, tail_lng)
497 /* same prefix: return the current class */
498 if (length == q_list->length)
501 /* found shorter common prefix */
503 /* add a node above q_list */
504 tmp = (struct q_class_t *) mem_calloc (1,
507 tmp->prefix = (char *) mem_calloc (1, length + 1);
508 strncpy (tmp->prefix, qptr, length);
509 tmp->length = length;
511 /* replace q_list by tmp */
513 tmp->next = q_list->next;
514 q_list->next->prev = tmp;
517 tmp->prev = q_list->prev;
518 q_list->prev->next = tmp;
521 /* make q_list a child of tmp */
523 tmp->up = q_list->up;
525 if (tmp->up->down == q_list)
528 /* q_list has no siblings */
532 index = q_list->index;
534 /* tmp should be the return class too */
537 /* next class to test */
541 /* found another branch for which tmp is a shorter prefix */
543 /* save the next sibling for later */
546 /* unlink q_list from the top level list */
548 q_list->next->prev = q_list->prev;
550 q_list->prev->next = q_list->next;
552 /* at this point, we have a tmp->down; link q_list to it */
561 index = q_list->index;
563 /* next class to test */
567 /* we found a shorter prefix, so we need a redraw */
572 q_list = q_list->next;
577 /* longer than the current prefix: try subclassing it */
579 && str_ncmp (tail_qptr, (q_list->prefix) + offset,
580 q_list->length - offset) == 0) {
581 /* still a subclass: go down one level */
583 offset = q_list->length;
585 q_list = q_list->down;
586 tail_lng = length - offset;
587 tail_qptr = (char *) qptr + offset;
592 /* nope, try the next prefix */
593 q_list = q_list->next;
599 /* still not found so far: add it as a sibling to the current node */
602 (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
603 tmp->prefix = (char *) mem_calloc (1, length + 1);
604 strncpy (tmp->prefix, qptr, length);
605 tmp->length = length;
608 tmp->next = ptr->down;
609 ptr->down->prev = tmp;
614 new_class_color (tmp, q_level);
620 shift_class_colors (*QuoteList, tmp, index, q_level);
626 /* nope, try the next prefix */
627 q_list = q_list->next;
634 /* not found so far: add it as a top level class */
635 class = (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
636 class->prefix = (char *) mem_calloc (1, length + 1);
637 strncpy (class->prefix, qptr, length);
638 class->length = length;
639 new_class_color (class, q_level);
642 class->next = *QuoteList;
643 (*QuoteList)->prev = class;
649 shift_class_colors (*QuoteList, tmp, index, q_level);
654 static int brailleLine = -1;
655 static int brailleCol = -1;
657 static int check_attachment_marker (char *);
660 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
661 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
664 COLOR_LINE *color_line;
665 regmatch_t pmatch[1], smatch[1];
666 int found, offset, null_rx, i;
668 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
669 if (buf[0] == '\n') {
670 lineInfo[n].type = MT_COLOR_NORMAL;
671 getyx(stdscr, brailleLine, brailleCol);
673 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
674 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
675 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
678 lineInfo[n].type = MT_COLOR_HDEFAULT;
679 color_line = ColorHdrList;
681 if (REGEXEC (&color_line->rx, buf) == 0) {
682 lineInfo[n].type = MT_COLOR_HEADER;
683 lineInfo[n].syntax[0].color = color_line->pair;
686 color_line = color_line->next;
690 else if (str_ncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
691 lineInfo[n].type = MT_COLOR_NORMAL;
693 else if (str_ncmp ("[-- ", buf, 4) == 0)
694 lineInfo[n].type = MT_COLOR_ATTACHMENT;
696 else if (check_attachment_marker ((char *) raw) == 0)
697 lineInfo[n].type = MT_COLOR_ATTACHMENT;
699 else if (str_cmp ("-- \n", buf) == 0
700 || str_cmp ("-- \r\n", buf) == 0) {
703 lineInfo[n].type = MT_COLOR_SIGNATURE;
704 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
705 (lineInfo[i].type == MT_COLOR_NORMAL ||
706 lineInfo[i].type == MT_COLOR_QUOTED ||
707 lineInfo[i].type == MT_COLOR_HEADER)) {
709 if (lineInfo[i].chunks) {
710 lineInfo[i].chunks = 0;
711 mem_realloc (&(lineInfo[n].syntax), sizeof (struct syntax_t));
713 lineInfo[i++].type = MT_COLOR_SIGNATURE;
716 else if (check_sig (buf, lineInfo, n - 1) == 0)
717 lineInfo[n].type = MT_COLOR_SIGNATURE;
718 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
719 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
720 if (smatch[0].rm_so > 0) {
723 /* hack to avoid making an extra copy of buf */
724 c = buf[smatch[0].rm_so];
725 buf[smatch[0].rm_so] = 0;
727 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
728 if (q_classify && lineInfo[n].quote == NULL)
729 lineInfo[n].quote = classify_quote (QuoteList,
730 buf + pmatch[0].rm_so,
732 pmatch[0].rm_so, force_redraw,
734 lineInfo[n].type = MT_COLOR_QUOTED;
737 lineInfo[n].type = MT_COLOR_NORMAL;
739 buf[smatch[0].rm_so] = c;
742 lineInfo[n].type = MT_COLOR_NORMAL;
745 if (q_classify && lineInfo[n].quote == NULL)
746 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
747 pmatch[0].rm_eo - pmatch[0].rm_so,
748 force_redraw, q_level);
749 lineInfo[n].type = MT_COLOR_QUOTED;
753 lineInfo[n].type = MT_COLOR_NORMAL;
756 if (lineInfo[n].type == MT_COLOR_NORMAL ||
757 lineInfo[n].type == MT_COLOR_QUOTED) {
761 lineInfo[n].chunks = 0;
768 color_line = ColorBodyList;
770 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
771 (offset ? REG_NOTBOL : 0)) == 0) {
772 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
774 if (++(lineInfo[n].chunks) > 1)
775 mem_realloc (&(lineInfo[n].syntax),
776 (lineInfo[n].chunks) *
777 sizeof (struct syntax_t));
779 i = lineInfo[n].chunks - 1;
780 pmatch[0].rm_so += offset;
781 pmatch[0].rm_eo += offset;
783 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
784 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
785 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
786 (lineInfo[n].syntax)[i].color = color_line->pair;
787 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
788 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
794 null_rx = 1; /* empty regexp; don't add it, but keep looking */
796 color_line = color_line->next;
800 offset++; /* avoid degenerate cases */
802 offset = (lineInfo[n].syntax)[i].last;
803 } while (found || null_rx);
807 static int is_ansi (unsigned char *buf)
809 while (*buf && (isdigit (*buf) || *buf == ';'))
811 return (*buf == 'm');
814 static int check_attachment_marker (char *p)
816 char *q = AttachmentMarker;
818 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
819 return (int) (*p - *q);
822 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
826 while (isdigit (buf[x]) || buf[x] == ';')
829 /* Character Attributes */
830 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
834 mutt_free_color (a->fg, a->bg);
842 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
843 a->attr |= ANSI_BOLD;
846 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
847 a->attr |= ANSI_UNDERLINE;
850 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
851 a->attr |= ANSI_BLINK;
854 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
855 a->attr |= ANSI_REVERSE;
858 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
863 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
866 mutt_free_color (a->fg, a->bg);
869 a->attr |= ANSI_COLOR;
870 if (buf[pos + 1] != '9')
871 a->fg = buf[pos + 1] - '0';
874 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
877 mutt_free_color (a->fg, a->bg);
880 a->attr |= ANSI_COLOR;
881 if (buf[pos + 1] != '9')
882 a->bg = buf[pos + 1] - '0';
886 while (pos < x && buf[pos] != ';')
896 /* trim tail of buf so that it contains complete multibyte characters */
897 static int trim_incomplete_mbyte(unsigned char *buf, size_t len) {
901 memset (&mbstate, 0, sizeof (mbstate));
902 for (; len > 0; buf += k, len -= k) {
903 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
906 else if (k == -1 || k == 0)
915 fill_buffer (FILE * f, off_t *last_pos, off_t offset, unsigned char *buf,
916 unsigned char *fmt, size_t blen, int *buf_ready)
919 static int b_read = 0;
921 if (*buf_ready == 0) {
923 if (offset != *last_pos)
924 fseeko (f, offset, 0);
925 if (fgets ((char *) buf, blen - 1, f) == NULL) {
929 *last_pos = ftello (f);
930 b_read = (int) (*last_pos - offset);
933 /* incomplete mbyte characters trigger a segfault in regex processing for
934 * certain versions of glibc. Trim them if necessary. */
935 if (b_read == blen - 2)
936 b_read -= trim_incomplete_mbyte(buf, b_read);
938 /* copy "buf" to "fmt", but without bold and underline controls */
941 if (*p == '\010' && (p > buf)) {
942 if (*(p + 1) == '_') /* underline */
944 else if (*(p + 1)) { /* bold or overstrike */
945 *(fmt - 1) = *(p + 1);
951 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
952 while (*p++ != 'm') /* skip ANSI sequence */
955 else if (*p == '\033' && *(p + 1) == ']'
956 && check_attachment_marker ((char *) p) == 0) {
957 debug_print (2, ("seen attachment marker.\n"));
958 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
975 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
976 int flags, ansi_attr * pa, int cnt,
977 int *pspace, int *pvch, int *pcol, int *pspecial)
979 int space = -1; /* index of the last space or TAB */
980 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
981 int ch, vch, k, last_special = -1, special = 0, t;
985 int wrap_cols = COLS;
987 if (!(flags & (M_SHOWFLAT)))
988 wrap_cols -= WrapMargin;
994 /* FIXME: this should come from lineInfo */
995 memset (&mbstate, 0, sizeof (mbstate));
997 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
998 /* Handle ANSI sequences */
999 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
1000 is_ansi (buf + ch + 2))
1001 ch = grok_ansi (buf, ch + 2, pa) + 1;
1003 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
1004 check_attachment_marker ((char *) buf + ch) == 0) {
1005 while (buf[ch++] != '\a')
1010 /* is anything left to do? */
1014 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
1015 if (k == -2 || k == -1) {
1016 debug_print (1, ("mbrtowc returned %d; errno = %d.\n", k, errno));
1017 if (col + 4 > wrap_cols)
1021 printw ("\\%03o", buf[ch]);
1028 /* Handle backspace */
1030 if (IsWPrint (wc)) {
1035 while ((wc1 = 0, mbstate1 = mbstate,
1037 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
1038 &mbstate1), k1 - k > 0 && wc1 == '\b')
1040 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1041 &mbstate1), k2 > 0 && IsWPrint (wc1))) {
1043 special |= (wc == '_' && special & A_UNDERLINE)
1044 ? A_UNDERLINE : A_BOLD;
1046 else if (wc == '_' || wc1 == '_') {
1047 special |= A_UNDERLINE;
1048 wc = (wc1 == '_') ? wc : wc1;
1051 /* special = 0; / * overstrike: nothing to do! */
1061 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1062 special || last_special || pa->attr)) {
1063 resolve_color (*lineInfo, n, vch, flags, special, pa);
1064 last_special = special;
1067 if (IsWPrint (wc)) {
1071 if (col + t > wrap_cols)
1077 else if (wc == '\n')
1079 else if (wc == '\t') {
1085 for (; col < t; col++)
1090 else if (wc < 0x20 || wc == 0x7f) {
1091 if (col + 2 > wrap_cols)
1095 printw ("^%c", ('@' + wc) & 0x7f);
1097 else if (wc < 0x100) {
1098 if (col + 4 > wrap_cols)
1102 printw ("\\%03o", wc);
1105 if (col + 1 > wrap_cols)
1109 addch (replacement_char ());
1115 *pspecial = special;
1121 * flags M_SHOWFLAT, show characters (used for displaying help)
1122 * M_SHOWCOLOR, show characters in color
1123 * otherwise don't show characters
1124 * M_HIDE, don't show quoted text
1125 * M_SEARCH, resolve search patterns
1126 * M_TYPES, compute line's type
1127 * M_PAGER_NSKIP, keeps leading whitespace
1128 * M_PAGER_MARKER, eventually show markers
1131 * -1 EOF was reached
1132 * 0 normal exit, line was not displayed
1133 * >0 normal exit, line was displayed
1137 display_line (FILE * f, off_t *last_pos, struct line_t **lineInfo, int n,
1138 int *last, int *max, int flags, struct q_class_t **QuoteList,
1139 int *q_level, int *force_redraw, regex_t * SearchRE)
1141 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1142 unsigned char *buf_ptr = buf;
1143 int ch, vch, col, cnt, b_read;
1144 int buf_ready = 0, change_last = 0;
1149 ansi_attr a = { 0, 0, 0, -1 };
1150 regmatch_t pmatch[1];
1157 if (*last == *max) {
1158 mem_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1159 for (ch = *last; ch < *max; ch++) {
1160 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1161 (*lineInfo)[ch].type = -1;
1162 (*lineInfo)[ch].search_cnt = -1;
1163 (*lineInfo)[ch].syntax = p_new(struct syntax_t, 1);
1164 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1169 /* only do color hiliting if we are viewing a message */
1170 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1171 if ((*lineInfo)[n].type == -1) {
1172 /* determine the line class */
1174 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1181 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1182 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1184 /* avoid race condition for continuation lines when scrolling up */
1186 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1188 (*lineInfo)[m].type = (*lineInfo)[n].type;
1191 /* this also prevents searching through the hidden lines */
1192 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1193 flags = 0; /* M_NOSHOW */
1196 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1197 * don't want to compute it every time M_TYPES is set, since this
1198 * would slow down the "bottom" function unacceptably. A compromise
1199 * solution is hence to call regexec() again, just to find out the
1200 * length of the quote prefix.
1202 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1203 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1206 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1212 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1213 (*lineInfo)[n].quote = classify_quote (QuoteList,
1214 (char *) fmt + pmatch[0].rm_so,
1215 pmatch[0].rm_eo - pmatch[0].rm_so,
1216 force_redraw, q_level);
1219 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1220 && (*lineInfo)[n].search_cnt == -1) {
1222 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1230 (*lineInfo)[n].search_cnt = 0;
1232 (SearchRE, (char *) fmt + offset, 1, pmatch,
1233 (offset ? REG_NOTBOL : 0)) == 0) {
1234 if (++((*lineInfo)[n].search_cnt) > 1)
1235 mem_realloc (&((*lineInfo)[n].search),
1236 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1238 (*lineInfo)[n].search = p_new(struct syntax_t, 1);
1239 pmatch[0].rm_so += offset;
1240 pmatch[0].rm_eo += offset;
1241 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1243 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1246 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1247 offset++; /* avoid degenerate cases */
1249 offset = pmatch[0].rm_eo;
1255 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1256 /* we've already scanned this line, so just exit */
1259 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1260 /* no need to try to display this line... */
1261 return (1); /* fake display */
1264 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1265 sizeof (buf), &buf_ready)) < 0) {
1271 /* now chose a good place to break the line */
1273 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1275 buf_ptr = buf + cnt;
1277 /* move the break point only if smart_wrap is set */
1278 if (option (OPTWRAP)) {
1280 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1281 && buf[cnt] != '\r') {
1283 /* skip trailing blanks */
1284 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1286 /* a very long word with leading spaces causes infinite wrapping */
1287 if ((!ch) && (flags & M_PAGER_NSKIP))
1288 buf_ptr = buf + cnt;
1293 buf_ptr = buf + cnt; /* a very long word... */
1295 if (!(flags & M_PAGER_NSKIP))
1296 /* skip leading blanks on the next line too */
1297 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1301 if (*buf_ptr == '\r')
1303 if (*buf_ptr == '\n')
1306 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1307 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1308 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1310 /* if we don't need to display the line we are done */
1311 if (!(flags & M_SHOW))
1314 /* display the line */
1315 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1317 /* avoid a bug in ncurses... */
1318 #ifndef USE_SLANG_CURSES
1320 SETCOLOR (MT_COLOR_NORMAL);
1325 /* end the last color pattern (needed by S-Lang) */
1326 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1327 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1330 * Fill the blank space at the end of the line with the prevailing color.
1331 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1332 * to make sure to reset the color *after* that
1334 if (flags & M_SHOWCOLOR) {
1335 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1336 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1337 def_color = ((*lineInfo)[m].syntax)[0].color;
1339 def_color = ColorDefs[(*lineInfo)[m].type];
1341 attrset (def_color);
1343 bkgdset (def_color | ' ');
1347 /* ncurses always wraps lines when you get to the right side of the
1348 * screen, but S-Lang seems to only wrap if the next character is *not*
1351 #ifndef USE_SLANG_CURSES
1357 * reset the color back to normal. This *must* come after the
1358 * addch('\n'), otherwise the color for this line will not be
1359 * filled to the right margin.
1361 if (flags & M_SHOWCOLOR) {
1362 SETCOLOR (MT_COLOR_NORMAL);
1363 BKGDSET (MT_COLOR_NORMAL);
1366 /* build a return code */
1367 if (!(flags & M_SHOW))
1373 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1375 while (cur > 0 && nlines > 0) {
1377 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1384 static struct mapping_t PagerHelp[] = {
1385 {N_("Exit"), OP_EXIT},
1386 {N_("PrevPg"), OP_PREV_PAGE},
1387 {N_("NextPg"), OP_NEXT_PAGE},
1390 static struct mapping_t PagerHelpExtra[] = {
1391 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1392 {N_("Del"), OP_DELETE},
1393 {N_("Reply"), OP_REPLY},
1394 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1399 static struct mapping_t PagerNewsHelpExtra[] = {
1400 {N_("Post"), OP_POST},
1401 {N_("Followup"), OP_FOLLOWUP},
1402 {N_("Del"), OP_DELETE},
1403 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1410 /* This pager is actually not so simple as it once was. It now operates in
1411 two modes: one for viewing messages and the other for viewing help. These
1412 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1413 is there so that we can do operations on the current message without the
1414 need to pop back out to the main-menu. */
1416 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1418 static char searchbuf[STRING];
1419 char buffer[LONG_STRING];
1420 char helpstr[SHORT_STRING * 2];
1421 char tmphelp[SHORT_STRING * 2];
1422 int maxLine, lastLine = 0;
1423 struct line_t *lineInfo;
1424 struct q_class_t *QuoteList = NULL;
1425 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1426 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1428 int redraw = REDRAW_FULL;
1430 off_t last_pos = 0, last_offset = 0;
1431 int old_smart_wrap, old_markers;
1434 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1435 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1437 int bodyoffset = 1; /* offset of first line of real text */
1438 int statusoffset = 0; /* offset for the status bar */
1439 int helpoffset = LINES - 2; /* offset for the help bar. */
1440 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1442 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1443 int indexoffset = 0; /* offset for the PI */
1444 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1445 int indicator = indexlen / 3; /* the indicator line of the PI */
1446 int old_PagerIndexLines; /* some people want to resize it
1447 * while inside the pager... */
1453 if (!(flags & M_SHOWCOLOR))
1454 flags |= M_SHOWFLAT;
1456 if ((fp = fopen (fname, "r")) == NULL) {
1457 mutt_perror (fname);
1461 if (stat (fname, &sb) != 0) {
1462 mutt_perror (fname);
1468 /* Initialize variables */
1470 if (IsHeader (extra) && !extra->hdr->read) {
1471 Context->msgnotreadyet = extra->hdr->msgno;
1472 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1475 lineInfo = p_new(struct line_t, maxLine = LINES);
1476 for (i = 0; i < maxLine; i++) {
1477 memset (&lineInfo[i], 0, sizeof (struct line_t));
1478 lineInfo[i].type = -1;
1479 lineInfo[i].search_cnt = -1;
1480 lineInfo[i].syntax = p_new(struct syntax_t, 1);
1481 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1484 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1485 if (IsHeader (extra)) {
1486 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1487 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1490 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1493 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1496 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1497 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1498 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1508 if (redraw & REDRAW_FULL) {
1509 SETCOLOR (MT_COLOR_NORMAL);
1510 /* clear() doesn't optimize screen redraws */
1514 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1515 indexlen = Context->vcount + 1;
1517 indexlen = PagerIndexLines;
1519 indicator = indexlen / 3;
1521 if (option (OPTSTATUSONTOP)) {
1523 statusoffset = IsHeader (extra) ? indexlen : 0;
1524 bodyoffset = statusoffset + 1;
1525 helpoffset = LINES - 2;
1526 bodylen = helpoffset - bodyoffset;
1527 if (!option (OPTHELP))
1533 statusoffset = LINES - 2;
1534 if (!option (OPTHELP))
1536 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1537 bodylen = statusoffset - bodyoffset;
1540 if (option (OPTHELP)) {
1541 SETCOLOR (MT_COLOR_STATUS);
1542 move (helpoffset, SW);
1543 mutt_paddstr (COLS-SW, helpstr);
1544 SETCOLOR (MT_COLOR_NORMAL);
1547 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1548 if (Resize != NULL) {
1549 if ((SearchCompiled = Resize->SearchCompiled)) {
1551 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1552 SearchFlag = M_SEARCH;
1553 SearchBack = Resize->SearchBack;
1555 lines = Resize->line;
1556 redraw |= REDRAW_SIGWINCH;
1562 if (IsHeader (extra) && PagerIndexLines) {
1563 if (index == NULL) {
1564 /* only allocate the space if/when we need the index.
1565 Initialise the menu as per the main index */
1566 index = mutt_new_menu ();
1567 index->menu = MENU_MAIN;
1568 index->make_entry = index_make_entry;
1569 index->color = index_color;
1570 index->max = Context->vcount;
1571 index->current = extra->hdr->virtual;
1574 SETCOLOR (MT_COLOR_NORMAL);
1575 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1577 index->pagelen = indexlen - 1;
1579 /* some fudge to work out where abouts the indicator should go */
1580 if (index->current - indicator < 0)
1582 else if (index->max - index->current < index->pagelen - indicator)
1583 index->top = index->max - index->pagelen;
1585 index->top = index->current - indicator;
1587 menu_redraw_index (index);
1590 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1594 if (redraw & REDRAW_SIGWINCH) {
1597 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1598 has_types | SearchFlag, &QuoteList, &q_level,
1599 &force_redraw, &SearchRE) == 0) {
1600 if (!lineInfo[i].continuation && ++j == lines) {
1605 redraw |= REDRAW_SIDEBAR;
1609 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1611 move (bodyoffset, SW);
1612 curline = oldtopline = topline;
1616 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1617 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1619 (flags & M_DISPLAYFLAGS) | hideQuoted |
1620 SearchFlag, &QuoteList, &q_level, &force_redraw,
1624 move (lines + bodyoffset, SW);
1625 redraw |= REDRAW_SIDEBAR;
1627 last_offset = lineInfo[curline].offset;
1628 } while (force_redraw);
1630 SETCOLOR (MT_COLOR_TILDE);
1631 BKGDSET (MT_COLOR_TILDE);
1632 while (lines < bodylen) {
1634 if (option (OPTTILDE))
1638 move (lines + bodyoffset, SW);
1640 /* We are going to update the pager status bar, so it isn't
1641 * necessary to reset to normal color now. */
1643 redraw |= REDRAW_STATUS; /* need to update the % seen */
1646 if (redraw & REDRAW_STATUS) {
1647 /* print out the pager status bar */
1648 SETCOLOR (MT_COLOR_STATUS);
1649 BKGDSET (MT_COLOR_STATUS);
1650 CLEARLINE_WIN (statusoffset);
1651 if (IsHeader (extra)) {
1652 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1653 size_t l2 = sizeof (buffer);
1655 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1656 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1658 else if (IsMsgAttach (extra)) {
1659 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1660 size_t l2 = sizeof (buffer);
1662 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1663 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1665 move(statusoffset,SW);
1666 mutt_paddstr (COLS - 10 - SW, IsHeader (extra) ||
1667 IsMsgAttach (extra) ? buffer : banner);
1670 if (last_pos < sb.st_size - 1)
1671 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1673 addstr (topline == 0 ? "all)" : "end)");
1674 BKGDSET (MT_COLOR_NORMAL);
1675 SETCOLOR (MT_COLOR_NORMAL);
1678 if (redraw & REDRAW_SIDEBAR)
1679 sidebar_draw (MENU_PAGER);
1681 if ((redraw & REDRAW_INDEX) && index) {
1682 /* redraw the pager_index indicator, because the
1683 * flags for this message might have changed. */
1684 menu_redraw_current (index);
1685 sidebar_draw (MENU_PAGER);
1686 /* print out the index status bar */
1687 menu_status_line (buffer, sizeof (buffer), index, NONULL (Status));
1688 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), SW);
1689 SETCOLOR (MT_COLOR_STATUS);
1690 BKGDSET (MT_COLOR_STATUS);
1691 mutt_paddstr (COLS-SW, buffer);
1692 SETCOLOR (MT_COLOR_NORMAL);
1693 BKGDSET (MT_COLOR_NORMAL);
1695 /* if we're not using the index, update every time */
1697 sidebar_draw (MENU_PAGER);
1701 if (option(OPTBRAILLEFRIENDLY)) {
1702 if (brailleLine!=-1) {
1703 move(brailleLine+1, 0);
1707 move (statusoffset, COLS-1);
1710 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1711 && lineInfo[curline].offset < sb.st_size-1) {
1712 if (TopLine - topline > lines)
1721 ch = km_dokey (MENU_PAGER);
1723 mutt_clear_error ();
1730 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1731 else if (SigWinch) {
1732 mutt_resize_screen ();
1734 /* Store current position. */
1736 for (i = 0; i <= topline; i++)
1737 if (!lineInfo[i].continuation)
1740 if (flags & M_PAGER_RETWINCH) {
1741 Resize = p_new(struct resize, 1);
1743 Resize->line = lines;
1744 Resize->SearchCompiled = SearchCompiled;
1745 Resize->SearchBack = SearchBack;
1748 rc = OP_REFORMAT_WINCH;
1751 for (i = 0; i < maxLine; i++) {
1752 lineInfo[i].offset = 0;
1753 lineInfo[i].type = -1;
1754 lineInfo[i].continuation = 0;
1755 lineInfo[i].chunks = 0;
1756 lineInfo[i].search_cnt = -1;
1757 lineInfo[i].quote = NULL;
1759 mem_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
1760 if (SearchCompiled && lineInfo[i].search)
1761 p_delete(&(lineInfo[i].search));
1767 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1772 clearok (stdscr, TRUE); /*force complete redraw */
1776 else if (ch == -1) {
1790 if (lineInfo[curline].offset < sb.st_size - 1) {
1791 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1793 else if (option (OPTPAGERSTOP)) {
1794 /* emulate "less -q" and don't go on to the next message. */
1795 mutt_error _("Bottom of message is shown.");
1798 /* end of the current message, so display the next message. */
1799 rc = OP_MAIN_NEXT_UNDELETED;
1807 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1810 mutt_error _("Top of message is shown.");
1814 if (lineInfo[curline].offset < sb.st_size - 1) {
1817 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1823 mutt_error _("Bottom of message is shown.");
1828 topline = upNLines (1, lineInfo, topline, hideQuoted);
1830 mutt_error _("Top of message is shown.");
1837 mutt_error _("Top of message is shown.");
1842 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1844 mutt_error _("Top of message is shown.");
1848 if (lineInfo[curline].offset < sb.st_size - 1) {
1849 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1851 else if (option (OPTPAGERSTOP)) {
1852 /* emulate "less -q" and don't go on to the next message. */
1853 mutt_error _("Bottom of message is shown.");
1856 /* end of the current message, so display the next message. */
1857 rc = OP_MAIN_NEXT_UNDELETED;
1862 case OP_SEARCH_NEXT:
1863 case OP_SEARCH_OPPOSITE:
1864 if (SearchCompiled) {
1866 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1867 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1868 /* searching forward */
1869 for (i = topline + 1; i < lastLine; i++) {
1870 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1871 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1878 mutt_error _("Not found.");
1881 /* searching backward */
1882 for (i = topline - 1; i >= 0; i--) {
1883 if ((!hideQuoted || (has_types &&
1884 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1885 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1892 mutt_error _("Not found.");
1895 if (lineInfo[topline].search_cnt > 0)
1896 SearchFlag = M_SEARCH;
1900 /* no previous search pattern, so fall through to search */
1903 case OP_SEARCH_REVERSE:
1904 strfcpy (buffer, searchbuf, sizeof (buffer));
1905 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1906 _("Search: ")), buffer, sizeof (buffer),
1910 if (!strcmp (buffer, searchbuf)) {
1911 if (SearchCompiled) {
1912 /* do an implicit search-next */
1913 if (ch == OP_SEARCH)
1914 ch = OP_SEARCH_NEXT;
1916 ch = OP_SEARCH_OPPOSITE;
1925 strfcpy (searchbuf, buffer, sizeof (searchbuf));
1927 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1928 if (ch == OP_SEARCH)
1930 else if (ch == OP_SEARCH_REVERSE)
1933 if (SearchCompiled) {
1934 regfree (&SearchRE);
1935 for (i = 0; i < lastLine; i++) {
1936 if (lineInfo[i].search)
1937 p_delete(&(lineInfo[i].search));
1938 lineInfo[i].search_cnt = -1;
1943 REGCOMP (&SearchRE, searchbuf,
1944 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1945 regerror (err, &SearchRE, buffer, sizeof (buffer));
1946 mutt_error ("%s", buffer);
1947 regfree (&SearchRE);
1948 for (i = 0; i < maxLine; i++) {
1950 if (lineInfo[i].search)
1951 p_delete(&(lineInfo[i].search));
1952 lineInfo[i].search_cnt = -1;
1959 /* update the search pointers */
1961 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1962 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1963 &QuoteList, &q_level,
1964 &force_redraw, &SearchRE) == 0) {
1966 redraw |= REDRAW_SIDEBAR;
1970 /* searching forward */
1971 for (i = topline; i < lastLine; i++) {
1972 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1973 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1981 /* searching backward */
1982 for (i = topline; i >= 0; i--) {
1983 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1984 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1992 if (lineInfo[topline].search_cnt == 0) {
1994 mutt_error _("Not found.");
1997 SearchFlag = M_SEARCH;
1999 redraw = REDRAW_BODY;
2002 case OP_SEARCH_TOGGLE:
2003 if (SearchCompiled) {
2004 SearchFlag ^= M_SEARCH;
2005 redraw = REDRAW_BODY;
2010 /* don't let the user enter the help-menu from the help screen! */
2013 mutt_help (MENU_PAGER);
2014 redraw = REDRAW_FULL;
2018 mutt_error _("Help is currently being shown.");
2021 case OP_PAGER_HIDE_QUOTED:
2023 hideQuoted ^= M_HIDE;
2024 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
2025 topline = upNLines (1, lineInfo, topline, hideQuoted);
2027 redraw = REDRAW_BODY;
2031 case OP_PAGER_SKIP_QUOTED:
2034 int new_topline = topline;
2036 while ((new_topline < lastLine ||
2037 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2038 new_topline, &lastLine,
2039 &maxLine, M_TYPES, &QuoteList,
2040 &q_level, &force_redraw,
2042 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
2043 redraw |= REDRAW_SIDEBAR;
2048 mutt_error _("No more quoted text.");
2053 while ((new_topline < lastLine ||
2054 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2055 new_topline, &lastLine,
2056 &maxLine, M_TYPES, &QuoteList,
2057 &q_level, &force_redraw,
2059 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2061 redraw |= REDRAW_SIDEBAR;
2065 mutt_error _("No more unquoted text after quoted text.");
2069 topline = new_topline;
2073 case OP_PAGER_BOTTOM: /* move to the end of the file */
2074 if (lineInfo[curline].offset < sb.st_size - 1) {
2076 /* make sure the types are defined to the end of file */
2077 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2078 &maxLine, has_types,
2079 &QuoteList, &q_level, &force_redraw,
2082 redraw |= REDRAW_SIDEBAR;
2084 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2087 mutt_error _("Bottom of message is shown.");
2091 clearok (stdscr, TRUE);
2092 redraw = REDRAW_FULL;
2096 km_error_key (MENU_PAGER);
2099 /* --------------------------------------------------------------------
2100 * The following are operations on the current message rather than
2101 * adjusting the view of the message.
2104 case OP_BOUNCE_MESSAGE:
2105 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2107 if (IsMsgAttach (extra))
2108 mutt_attach_bounce (extra->fp, extra->hdr,
2109 extra->idx, extra->idxlen, extra->bdy);
2111 ci_bounce_message (extra->hdr, &redraw);
2115 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2117 if (IsMsgAttach (extra))
2118 mutt_attach_resend (extra->fp, extra->hdr,
2119 extra->idx, extra->idxlen, extra->bdy);
2121 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2122 redraw = REDRAW_FULL;
2125 case OP_CHECK_TRADITIONAL:
2126 CHECK_MODE (IsHeader (extra));
2127 if (!(WithCrypto & APPLICATION_PGP))
2129 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2131 rc = OP_CHECK_TRADITIONAL;
2135 case OP_CREATE_ALIAS:
2136 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2137 if (IsMsgAttach (extra))
2138 mutt_create_alias (extra->bdy->hdr->env, NULL);
2140 mutt_create_alias (extra->hdr->env, NULL);
2141 MAYBE_REDRAW (redraw);
2144 case OP_PURGE_MESSAGE:
2146 CHECK_MODE (IsHeader (extra));
2149 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2151 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2152 mutt_set_flag (Context, extra->hdr, M_PURGED,
2153 ch != OP_PURGE_MESSAGE ? 0 : 1);
2154 if (option (OPTDELETEUNTAG))
2155 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2156 redraw = REDRAW_STATUS | REDRAW_INDEX;
2157 if (option (OPTRESOLVE)) {
2159 rc = OP_MAIN_NEXT_UNDELETED;
2163 case OP_DELETE_THREAD:
2164 case OP_DELETE_SUBTHREAD:
2165 CHECK_MODE (IsHeader (extra));
2168 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2170 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2171 ch == OP_DELETE_THREAD ? 0 : 1);
2174 if (option (OPTDELETEUNTAG))
2175 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2176 ch == OP_DELETE_THREAD ? 0 : 1);
2177 if (option (OPTRESOLVE)) {
2178 rc = OP_MAIN_NEXT_UNDELETED;
2182 if (!option (OPTRESOLVE) && PagerIndexLines)
2183 redraw = REDRAW_FULL;
2185 redraw = REDRAW_STATUS | REDRAW_INDEX;
2189 case OP_DISPLAY_ADDRESS:
2190 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2191 if (IsMsgAttach (extra))
2192 mutt_display_address (extra->bdy->hdr->env);
2194 mutt_display_address (extra->hdr->env);
2197 case OP_ENTER_COMMAND:
2198 old_smart_wrap = option (OPTWRAP);
2199 old_markers = option (OPTMARKERS);
2200 old_PagerIndexLines = PagerIndexLines;
2202 CurrentMenu = MENU_PAGER;
2203 mutt_enter_command ();
2205 if (option (OPTNEEDRESORT)) {
2206 unset_option (OPTNEEDRESORT);
2207 CHECK_MODE (IsHeader (extra));
2208 set_option (OPTNEEDRESORT);
2211 if (old_PagerIndexLines != PagerIndexLines) {
2213 mutt_menuDestroy (&index);
2217 if (option (OPTWRAP) != old_smart_wrap ||
2218 option (OPTMARKERS) != old_markers) {
2219 if (flags & M_PAGER_RETWINCH) {
2221 rc = OP_REFORMAT_WINCH;
2225 /* count the real lines above */
2227 for (i = 0; i <= topline; i++) {
2228 if (!lineInfo[i].continuation)
2232 /* we need to restart the whole thing */
2233 for (i = 0; i < maxLine; i++) {
2234 lineInfo[i].offset = 0;
2235 lineInfo[i].type = -1;
2236 lineInfo[i].continuation = 0;
2237 lineInfo[i].chunks = 0;
2238 lineInfo[i].search_cnt = -1;
2239 lineInfo[i].quote = NULL;
2241 mem_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2242 if (SearchCompiled && lineInfo[i].search)
2243 p_delete(&(lineInfo[i].search));
2246 if (SearchCompiled) {
2247 regfree (&SearchRE);
2252 /* try to keep the old position */
2255 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2256 &lastLine, &maxLine,
2257 (has_types ? M_TYPES : 0),
2258 &QuoteList, &q_level, &force_redraw,
2260 redraw |= REDRAW_SIDEBAR;
2261 if (!lineInfo[topline].continuation)
2270 if (option (OPTFORCEREDRAWPAGER))
2271 redraw = REDRAW_FULL;
2272 unset_option (OPTFORCEREDRAWINDEX);
2273 unset_option (OPTFORCEREDRAWPAGER);
2276 case OP_FLAG_MESSAGE:
2277 CHECK_MODE (IsHeader (extra));
2280 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2282 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2283 redraw = REDRAW_STATUS | REDRAW_INDEX;
2284 if (option (OPTRESOLVE)) {
2286 rc = OP_MAIN_NEXT_UNDELETED;
2291 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2292 if (IsAttach (extra))
2293 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2295 mutt_pipe_message (extra->hdr);
2296 MAYBE_REDRAW (redraw);
2300 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2301 if (IsAttach (extra))
2302 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2304 mutt_print_message (extra->hdr);
2308 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2310 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2311 redraw = REDRAW_FULL;
2316 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2318 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2319 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2320 query_quadoption (OPT_TOMODERATED,
2322 ("Posting to this group not allowed, may be moderated. Continue?"))
2325 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2326 redraw = REDRAW_FULL;
2329 case OP_FORWARD_TO_GROUP:
2330 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2332 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2333 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2334 query_quadoption (OPT_TOMODERATED,
2336 ("Posting to this group not allowed, may be moderated. Continue?"))
2339 if (IsMsgAttach (extra))
2340 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2341 extra->idxlen, extra->bdy, SENDNEWS);
2343 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2345 redraw = REDRAW_FULL;
2349 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2352 if (IsMsgAttach (extra))
2353 followup_to = extra->bdy->hdr->env->followup_to;
2355 followup_to = extra->hdr->env->followup_to;
2357 if (!followup_to || str_casecmp (followup_to, "poster") ||
2358 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2359 _("Reply by mail as poster prefers?")) != M_YES) {
2360 if (extra->ctx && extra->ctx->magic == M_NNTP
2361 && !((NNTP_DATA *) extra->ctx->data)->allowed
2362 && query_quadoption (OPT_TOMODERATED,
2364 ("Posting to this group not allowed, may be moderated. Continue?"))
2367 if (IsMsgAttach (extra))
2368 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2369 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2371 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2372 extra->ctx, extra->hdr);
2373 redraw = REDRAW_FULL;
2379 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2381 if (IsMsgAttach (extra))
2382 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2383 extra->idxlen, extra->bdy, SENDREPLY);
2385 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2386 redraw = REDRAW_FULL;
2389 case OP_RECALL_MESSAGE:
2390 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2392 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2393 redraw = REDRAW_FULL;
2396 case OP_GROUP_REPLY:
2397 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2399 if (IsMsgAttach (extra))
2400 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2401 extra->idxlen, extra->bdy,
2402 SENDREPLY | SENDGROUPREPLY);
2404 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2406 redraw = REDRAW_FULL;
2410 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2412 if (IsMsgAttach (extra))
2413 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2414 extra->idxlen, extra->bdy,
2415 SENDREPLY | SENDLISTREPLY);
2417 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2419 redraw = REDRAW_FULL;
2422 case OP_FORWARD_MESSAGE:
2423 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2425 if (IsMsgAttach (extra))
2426 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2427 extra->idxlen, extra->bdy, 0);
2429 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2430 redraw = REDRAW_FULL;
2433 case OP_DECRYPT_SAVE:
2440 if (IsAttach (extra)) {
2441 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2446 case OP_COPY_MESSAGE:
2447 case OP_DECODE_SAVE:
2448 case OP_DECODE_COPY:
2449 case OP_DECRYPT_COPY:
2450 if (!WithCrypto && ch == OP_DECRYPT_COPY) {
2454 CHECK_MODE (IsHeader (extra));
2455 if (mutt_save_message (extra->hdr,
2456 (ch == OP_DECRYPT_SAVE) ||
2457 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2458 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2459 (ch == OP_DECRYPT_SAVE)
2460 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2461 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2462 || ch == OP_DECRYPT_SAVE)) {
2463 if (option (OPTRESOLVE)) {
2465 rc = OP_MAIN_NEXT_UNDELETED;
2468 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2470 MAYBE_REDRAW (redraw);
2473 case OP_SHELL_ESCAPE:
2474 mutt_shell_escape ();
2475 MAYBE_REDRAW (redraw);
2479 CHECK_MODE (IsHeader (extra));
2480 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2482 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2483 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2484 ? NULL : Context->last_tag);
2486 redraw = REDRAW_STATUS | REDRAW_INDEX;
2487 if (option (OPTRESOLVE)) {
2494 CHECK_MODE (IsHeader (extra));
2497 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2499 if (extra->hdr->read || extra->hdr->old)
2500 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2502 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2504 Context->msgnotreadyet = -1;
2505 redraw = REDRAW_STATUS | REDRAW_INDEX;
2506 if (option (OPTRESOLVE)) {
2508 rc = OP_MAIN_NEXT_UNDELETED;
2513 CHECK_MODE (IsHeader (extra));
2516 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2518 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2519 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2520 redraw = REDRAW_STATUS | REDRAW_INDEX;
2521 if (option (OPTRESOLVE)) {
2527 case OP_UNDELETE_THREAD:
2528 case OP_UNDELETE_SUBTHREAD:
2529 CHECK_MODE (IsHeader (extra));
2532 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2534 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2535 ch == OP_UNDELETE_THREAD ? 0 : 1)
2536 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2537 ch == OP_UNDELETE_THREAD ? 0 : 1);
2540 if (option (OPTRESOLVE)) {
2541 rc = (ch == OP_DELETE_THREAD) ?
2542 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2546 if (!option (OPTRESOLVE) && PagerIndexLines)
2547 redraw = REDRAW_FULL;
2549 redraw = REDRAW_STATUS | REDRAW_INDEX;
2558 if (option (OPTFORCEBUFFYCHECK))
2561 redraw |= REDRAW_SIDEBAR;
2564 case OP_VIEW_ATTACHMENTS:
2565 if (flags & M_PAGER_ATTACHMENT) {
2567 rc = OP_ATTACH_COLLAPSE;
2570 CHECK_MODE (IsHeader (extra));
2571 mutt_view_attachments (extra->hdr);
2572 if (extra->hdr->attach_del)
2573 Context->changed = 1;
2574 redraw = REDRAW_FULL;
2579 if (!(WithCrypto & APPLICATION_PGP)) {
2583 CHECK_MODE (IsHeader (extra));
2585 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2586 redraw = REDRAW_FULL;
2590 case OP_FORGET_PASSPHRASE:
2591 crypt_forget_passphrase ();
2594 case OP_EXTRACT_KEYS:
2599 CHECK_MODE (IsHeader (extra));
2600 crypt_extract_keys_from_messages (extra->hdr);
2601 redraw = REDRAW_FULL;
2604 case OP_SIDEBAR_SCROLL_UP:
2605 case OP_SIDEBAR_SCROLL_DOWN:
2606 case OP_SIDEBAR_NEXT:
2607 case OP_SIDEBAR_NEXT_NEW:
2608 case OP_SIDEBAR_PREV:
2609 case OP_SIDEBAR_PREV_NEW:
2610 sidebar_scroll (ch, MENU_PAGER);
2619 if (IsHeader (extra)) {
2620 Context->msgnotreadyet = -1;
2625 OldHdr = extra->hdr;
2629 cleanup_quote (&QuoteList);
2631 for (i = 0; i < maxLine; i++) {
2632 p_delete(&(lineInfo[i].syntax));
2633 if (SearchCompiled && lineInfo[i].search)
2634 p_delete(&(lineInfo[i].search));
2636 if (SearchCompiled) {
2637 regfree (&SearchRE);
2640 p_delete(&lineInfo);
2642 mutt_menuDestroy (&index);
2643 return (rc != -1 ? rc : 0);