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)
818 lineInfo[n].chunks = 0;
826 color_line = ColorBodyList;
829 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
830 (offset ? REG_NOTBOL : 0)) == 0)
832 if (pmatch[0].rm_eo != pmatch[0].rm_so)
836 if (++(lineInfo[n].chunks) > 1)
837 safe_realloc (&(lineInfo[n].syntax),
838 (lineInfo[n].chunks) * sizeof (struct syntax_t));
840 i = lineInfo[n].chunks - 1;
841 pmatch[0].rm_so += offset;
842 pmatch[0].rm_eo += offset;
844 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
845 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
846 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last))
848 (lineInfo[n].syntax)[i].color = color_line->pair;
849 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
850 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
856 null_rx = 1; /* empty regexp; don't add it, but keep looking */
858 color_line = color_line->next;
862 offset++; /* avoid degenerate cases */
864 offset = (lineInfo[n].syntax)[i].last;
865 } while (found || null_rx);
869 static int is_ansi (unsigned char *buf)
871 while (*buf && (isdigit(*buf) || *buf == ';'))
873 return (*buf == 'm');
876 static int check_attachment_marker (char *p)
878 char *q = AttachmentMarker;
880 for (;*p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++)
882 return (int) (*p - *q);
885 static int grok_ansi(unsigned char *buf, int pos, ansi_attr *a)
889 while (isdigit(buf[x]) || buf[x] == ';')
892 /* Character Attributes */
893 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm')
899 mutt_free_color (a->fg, a->bg);
906 if (buf[pos] == '1' && (pos+1 == x || buf[pos+1] == ';'))
908 a->attr |= ANSI_BOLD;
911 else if (buf[pos] == '4' && (pos+1 == x || buf[pos+1] == ';'))
913 a->attr |= ANSI_UNDERLINE;
916 else if (buf[pos] == '5' && (pos+1 == x || buf[pos+1] == ';'))
918 a->attr |= ANSI_BLINK;
921 else if (buf[pos] == '7' && (pos+1 == x || buf[pos+1] == ';'))
923 a->attr |= ANSI_REVERSE;
926 else if (buf[pos] == '0' && (pos+1 == x || buf[pos+1] == ';'))
930 mutt_free_color(a->fg,a->bg);
936 else if (buf[pos] == '3' && isdigit(buf[pos+1]))
940 mutt_free_color(a->fg,a->bg);
943 a->attr |= ANSI_COLOR;
944 a->fg = buf[pos+1] - '0';
947 else if (buf[pos] == '4' && isdigit(buf[pos+1]))
951 mutt_free_color(a->fg,a->bg);
954 a->attr |= ANSI_COLOR;
955 a->bg = buf[pos+1] - '0';
960 while (pos < x && buf[pos] != ';') pos++;
969 /* trim tail of buf so that it contains complete multibyte characters */
971 trim_incomplete_mbyte(unsigned char *buf, size_t len)
976 memset (&mbstate, 0, sizeof (mbstate));
977 for (; len > 0; buf += k, len -= k)
979 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
982 else if (k == -1 || k == 0)
991 fill_buffer (FILE *f, LOFF_T *last_pos, LOFF_T offset, unsigned char *buf,
992 unsigned char *fmt, size_t blen, int *buf_ready)
1000 if (offset != *last_pos)
1001 fseeko (f, offset, 0);
1002 if (fgets ((char *) buf, blen - 1, f) == NULL)
1007 *last_pos = ftello (f);
1008 b_read = (int) (*last_pos - offset);
1011 /* incomplete mbyte characters trigger a segfault in regex processing for
1012 * certain versions of glibc. Trim them if necessary. */
1013 if (b_read == blen - 2)
1014 b_read -= trim_incomplete_mbyte(buf, b_read);
1016 /* copy "buf" to "fmt", but without bold and underline controls */
1020 if (*p == '\010' && (p > buf))
1022 if (*(p+1) == '_') /* underline */
1024 else if (*(p+1)) /* bold or overstrike */
1032 else if (*p == '\033' && *(p+1) == '[' && is_ansi (p + 2))
1034 while (*p++ != 'm') /* skip ANSI sequence */
1037 else if (*p == '\033' && *(p+1) == ']' && check_attachment_marker ((char *) p) == 0)
1039 dprint (2, (debugfile, "fill_buffer: Seen attachment marker.\n"));
1040 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
1052 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
1053 int flags, ansi_attr *pa, int cnt,
1054 int *pspace, int *pvch, int *pcol, int *pspecial)
1056 int space = -1; /* index of the last space or TAB */
1057 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
1058 int ch, vch, k, last_special = -1, special = 0, t;
1062 int wrap_cols = mutt_term_width ((flags & M_PAGER_NOWRAP) ? 0 : Wrap);
1064 /* FIXME: this should come from lineInfo */
1065 memset(&mbstate, 0, sizeof(mbstate));
1067 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k)
1069 /* Handle ANSI sequences */
1070 while (cnt-ch >= 2 && buf[ch] == '\033' && buf[ch+1] == '[' &&
1072 ch = grok_ansi (buf, ch+2, pa) + 1;
1074 while (cnt-ch >= 2 && buf[ch] == '\033' && buf[ch+1] == ']' &&
1075 check_attachment_marker ((char *) buf+ch) == 0)
1077 while (buf[ch++] != '\a')
1082 /* is anything left to do? */
1086 k = mbrtowc (&wc, (char *)buf+ch, cnt-ch, &mbstate);
1087 if (k == -2 || k == -1)
1089 dprint (1, (debugfile, "%s:%d: mbrtowc returned %d; errno = %d.\n",
1090 __FILE__, __LINE__, k, errno));
1091 if (col + 4 > wrap_cols)
1095 printw ("\\%03o", buf[ch]);
1102 if (Charset_is_utf8 && (wc == 0x200B || wc == 0xFEFF))
1104 dprint (3, (debugfile, "skip zero-width character U+%04X\n", (unsigned short)wc));
1108 /* Handle backspace */
1116 while ((wc1 = 0, mbstate1 = mbstate,
1117 k1 = k + mbrtowc (&wc1, (char *)buf+ch+k, cnt-ch-k, &mbstate1),
1118 k1 - k > 0 && wc1 == '\b') &&
1120 k2 = mbrtowc (&wc1, (char *)buf+ch+k1, cnt-ch-k1, &mbstate1),
1121 k2 > 0 && IsWPrint (wc1)))
1125 special |= (wc == '_' && special & A_UNDERLINE)
1126 ? A_UNDERLINE : A_BOLD;
1128 else if (wc == '_' || wc1 == '_')
1130 special |= A_UNDERLINE;
1131 wc = (wc1 == '_') ? wc : wc1;
1135 /* special = 0; / * overstrike: nothing to do! */
1145 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1146 special || last_special || pa->attr))
1148 resolve_color (*lineInfo, n, vch, flags, special, pa);
1149 last_special = special;
1157 if (col + t > wrap_cols)
1163 else if (wc == '\n')
1165 else if (wc == '\t')
1172 for (; col < t; col++)
1177 else if (wc < 0x20 || wc == 0x7f)
1179 if (col + 2 > wrap_cols)
1183 printw ("^%c", ('@' + wc) & 0x7f);
1185 else if (wc < 0x100)
1187 if (col + 4 > wrap_cols)
1191 printw ("\\%03o", wc);
1195 if (col + 1 > wrap_cols)
1199 addch (replacement_char ());
1205 *pspecial = special;
1211 * flags M_SHOWFLAT, show characters (used for displaying help)
1212 * M_SHOWCOLOR, show characters in color
1213 * otherwise don't show characters
1214 * M_HIDE, don't show quoted text
1215 * M_SEARCH, resolve search patterns
1216 * M_TYPES, compute line's type
1217 * M_PAGER_NSKIP, keeps leading whitespace
1218 * M_PAGER_MARKER, eventually show markers
1221 * -1 EOF was reached
1222 * 0 normal exit, line was not displayed
1223 * >0 normal exit, line was displayed
1227 display_line (FILE *f, LOFF_T *last_pos, struct line_t **lineInfo, int n,
1228 int *last, int *max, int flags, struct q_class_t **QuoteList,
1229 int *q_level, int *force_redraw, regex_t *SearchRE)
1231 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1232 unsigned char *buf_ptr = buf;
1233 int ch, vch, col, cnt, b_read;
1234 int buf_ready = 0, change_last = 0;
1239 ansi_attr a = {0,0,0,-1};
1240 regmatch_t pmatch[1];
1250 safe_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1251 for (ch = *last; ch < *max ; ch++)
1253 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1254 (*lineInfo)[ch].type = -1;
1255 (*lineInfo)[ch].search_cnt = -1;
1256 (*lineInfo)[ch].syntax = safe_malloc (sizeof (struct syntax_t));
1257 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last = -1;
1261 /* only do color hiliting if we are viewing a message */
1262 if (flags & (M_SHOWCOLOR | M_TYPES))
1264 if ((*lineInfo)[n].type == -1)
1266 /* determine the line class */
1267 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1274 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1275 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1277 /* avoid race condition for continuation lines when scrolling up */
1278 for (m = n + 1; m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation; m++)
1279 (*lineInfo)[m].type = (*lineInfo)[n].type;
1282 /* this also prevents searching through the hidden lines */
1283 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1284 flags = 0; /* M_NOSHOW */
1287 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1288 * don't want to compute it every time M_TYPES is set, since this
1289 * would slow down the "bottom" function unacceptably. A compromise
1290 * solution is hence to call regexec() again, just to find out the
1291 * length of the quote prefix.
1293 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1294 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1296 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1302 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1303 (*lineInfo)[n].quote = classify_quote (QuoteList,
1304 (char *) fmt + pmatch[0].rm_so,
1305 pmatch[0].rm_eo - pmatch[0].rm_so,
1306 force_redraw, q_level);
1309 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation && (*lineInfo)[n].search_cnt == -1)
1311 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1319 (*lineInfo)[n].search_cnt = 0;
1320 while (regexec (SearchRE, (char *) fmt + offset, 1, pmatch, (offset ? REG_NOTBOL : 0)) == 0)
1322 if (++((*lineInfo)[n].search_cnt) > 1)
1323 safe_realloc (&((*lineInfo)[n].search),
1324 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1326 (*lineInfo)[n].search = safe_malloc (sizeof (struct syntax_t));
1327 pmatch[0].rm_so += offset;
1328 pmatch[0].rm_eo += offset;
1329 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first = pmatch[0].rm_so;
1330 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last = pmatch[0].rm_eo;
1332 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1333 offset++; /* avoid degenerate cases */
1335 offset = pmatch[0].rm_eo;
1341 if (!(flags & M_SHOW) && (*lineInfo)[n+1].offset > 0)
1343 /* we've already scanned this line, so just exit */
1346 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n+1].offset > 0)
1348 /* no need to try to display this line... */
1349 return (1); /* fake display */
1352 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1353 sizeof (buf), &buf_ready)) < 0)
1360 /* now chose a good place to break the line */
1361 cnt = format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col, &special);
1362 buf_ptr = buf + cnt;
1364 /* move the break point only if smart_wrap is set */
1365 if (option (OPTWRAP))
1369 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n' && buf[cnt] != '\r')
1372 /* skip trailing blanks */
1373 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1375 /* a very long word with leading spaces causes infinite wrapping */
1376 if ((!ch) && (flags & M_PAGER_NSKIP))
1377 buf_ptr = buf + cnt;
1382 buf_ptr = buf + cnt; /* a very long word... */
1384 if (!(flags & M_PAGER_NSKIP))
1385 /* skip leading blanks on the next line too */
1386 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1390 if (*buf_ptr == '\r')
1392 if (*buf_ptr == '\n')
1395 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n+1].continuation)
1396 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1397 (*lineInfo)[n+1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1399 /* if we don't need to display the line we are done */
1400 if (!(flags & M_SHOW))
1403 /* display the line */
1404 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1406 /* avoid a bug in ncurses... */
1407 #ifndef USE_SLANG_CURSES
1410 SETCOLOR (MT_COLOR_NORMAL);
1415 /* end the last color pattern (needed by S-Lang) */
1416 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1417 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1420 * Fill the blank space at the end of the line with the prevailing color.
1421 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1422 * to make sure to reset the color *after* that
1424 if (flags & M_SHOWCOLOR)
1426 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1427 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1428 def_color = ((*lineInfo)[m].syntax)[0].color;
1430 def_color = ColorDefs[ (*lineInfo)[m].type ];
1432 attrset (def_color);
1434 bkgdset (def_color | ' ');
1438 /* ncurses always wraps lines when you get to the right side of the
1439 * screen, but S-Lang seems to only wrap if the next character is *not*
1442 #ifndef USE_SLANG_CURSES
1448 * reset the color back to normal. This *must* come after the
1449 * addch('\n'), otherwise the color for this line will not be
1450 * filled to the right margin.
1452 if (flags & M_SHOWCOLOR)
1454 SETCOLOR(MT_COLOR_NORMAL);
1455 BKGDSET(MT_COLOR_NORMAL);
1458 /* build a return code */
1459 if (!(flags & M_SHOW))
1466 upNLines (int nlines, struct line_t *info, int cur, int hiding)
1468 while (cur > 0 && nlines > 0)
1471 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1478 static struct mapping_t PagerHelp[] = {
1479 { N_("Exit"), OP_EXIT },
1480 { N_("PrevPg"), OP_PREV_PAGE },
1481 { N_("NextPg"), OP_NEXT_PAGE },
1484 static struct mapping_t PagerHelpExtra[] = {
1485 { N_("View Attachm."), OP_VIEW_ATTACHMENTS },
1486 { N_("Del"), OP_DELETE },
1487 { N_("Reply"), OP_REPLY },
1488 { N_("Next"), OP_MAIN_NEXT_UNDELETED },
1494 /* This pager is actually not so simple as it once was. It now operates in
1495 two modes: one for viewing messages and the other for viewing help. These
1496 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1497 is there so that we can do operations on the current message without the
1498 need to pop back out to the main-menu. */
1500 mutt_pager (const char *banner, const char *fname, int flags, pager_t *extra)
1502 static char searchbuf[STRING];
1503 char buffer[LONG_STRING];
1504 char helpstr[SHORT_STRING*2];
1505 char tmphelp[SHORT_STRING*2];
1506 int maxLine, lastLine = 0;
1507 struct line_t *lineInfo;
1508 struct q_class_t *QuoteList = NULL;
1509 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1510 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1511 int r = -1, wrapped = 0;
1512 int redraw = REDRAW_FULL;
1514 LOFF_T last_pos = 0, last_offset = 0;
1515 int old_smart_wrap, old_markers;
1518 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1519 int has_types = (IsHeader(extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1521 int bodyoffset = 1; /* offset of first line of real text */
1522 int statusoffset = 0; /* offset for the status bar */
1523 int helpoffset = LINES - 2; /* offset for the help bar. */
1524 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1526 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1527 int indexoffset = 0; /* offset for the PI */
1528 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1529 int indicator = indexlen / 3; /* the indicator line of the PI */
1530 int old_PagerIndexLines; /* some people want to resize it
1531 * while inside the pager... */
1533 if (!(flags & M_SHOWCOLOR))
1534 flags |= M_SHOWFLAT;
1536 if ((fp = fopen (fname, "r")) == NULL)
1538 mutt_perror (fname);
1542 if (stat (fname, &sb) != 0)
1544 mutt_perror (fname);
1550 /* Initialize variables */
1552 if (IsHeader (extra) && !extra->hdr->read)
1554 Context->msgnotreadyet = extra->hdr->msgno;
1555 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1558 lineInfo = safe_malloc (sizeof (struct line_t) * (maxLine = LINES));
1559 for (i = 0 ; i < maxLine ; i++)
1561 memset (&lineInfo[i], 0, sizeof (struct line_t));
1562 lineInfo[i].type = -1;
1563 lineInfo[i].search_cnt = -1;
1564 lineInfo[i].syntax = safe_malloc (sizeof (struct syntax_t));
1565 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1568 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1569 if (IsHeader (extra))
1571 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1572 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER, PagerHelpExtra);
1573 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1577 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1578 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1579 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1586 if (redraw & REDRAW_FULL)
1588 SETCOLOR (MT_COLOR_NORMAL);
1589 /* clear() doesn't optimize screen redraws */
1593 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1594 indexlen = Context->vcount + 1;
1596 indexlen = PagerIndexLines;
1598 indicator = indexlen / 3;
1600 if (option (OPTSTATUSONTOP))
1603 statusoffset = IsHeader (extra) ? indexlen : 0;
1604 bodyoffset = statusoffset + 1;
1605 helpoffset = LINES - 2;
1606 bodylen = helpoffset - bodyoffset;
1607 if (!option (OPTHELP))
1614 statusoffset = LINES - 2;
1615 if (!option (OPTHELP))
1617 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1618 bodylen = statusoffset - bodyoffset;
1621 if (option (OPTHELP))
1623 SETCOLOR (MT_COLOR_STATUS);
1624 move (helpoffset, 0);
1625 mutt_paddstr (COLS, helpstr);
1626 SETCOLOR (MT_COLOR_NORMAL);
1629 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1632 if ((SearchCompiled = Resize->SearchCompiled))
1635 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1636 SearchFlag = M_SEARCH;
1637 SearchBack = Resize->SearchBack;
1639 lines = Resize->line;
1640 redraw |= REDRAW_SIGWINCH;
1646 if (IsHeader (extra) && PagerIndexLines)
1650 /* only allocate the space if/when we need the index.
1651 Initialise the menu as per the main index */
1652 index = mutt_new_menu(MENU_MAIN);
1653 index->make_entry = index_make_entry;
1654 index->color = index_color;
1655 index->max = Context->vcount;
1656 index->current = extra->hdr->virtual;
1659 SETCOLOR (MT_COLOR_NORMAL);
1660 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1662 index->pagelen = indexlen - 1;
1664 /* some fudge to work out where abouts the indicator should go */
1665 if (index->current - indicator < 0)
1667 else if (index->max - index->current < index->pagelen - indicator)
1668 index->top = index->max - index->pagelen;
1670 index->top = index->current - indicator;
1672 menu_redraw_index(index);
1675 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1679 if (redraw & REDRAW_SIGWINCH)
1683 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1684 has_types | SearchFlag | (flags & M_PAGER_NOWRAP), &QuoteList, &q_level, &force_redraw,
1686 if (!lineInfo[i].continuation && ++j == lines)
1694 if ((redraw & REDRAW_BODY) || topline != oldtopline)
1697 move (bodyoffset, 0);
1698 curline = oldtopline = topline;
1702 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1)
1704 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1706 (flags & M_DISPLAYFLAGS) | hideQuoted | SearchFlag | (flags & M_PAGER_NOWRAP),
1707 &QuoteList, &q_level, &force_redraw, &SearchRE) > 0)
1711 last_offset = lineInfo[curline].offset;
1712 } while (force_redraw);
1714 SETCOLOR (MT_COLOR_TILDE);
1715 BKGDSET (MT_COLOR_TILDE);
1716 while (lines < bodylen)
1719 if (option (OPTTILDE))
1724 /* We are going to update the pager status bar, so it isn't
1725 * necessary to reset to normal color now. */
1727 redraw |= REDRAW_STATUS; /* need to update the % seen */
1730 if (redraw & REDRAW_STATUS)
1732 struct hdr_format_info hfi;
1733 char pager_progress_str[4];
1736 hfi.pager_progress = pager_progress_str;
1738 if (last_pos < sb.st_size - 1)
1739 snprintf(pager_progress_str, sizeof(pager_progress_str), OFF_T_FMT "%%", (100 * last_offset / sb.st_size));
1741 strfcpy(pager_progress_str, (topline == 0) ? "all" : "end", sizeof(pager_progress_str));
1743 /* print out the pager status bar */
1744 SETCOLOR (MT_COLOR_STATUS);
1745 BKGDSET (MT_COLOR_STATUS);
1746 CLEARLINE (statusoffset);
1747 if (IsHeader (extra))
1749 size_t l1 = COLS * MB_LEN_MAX;
1750 size_t l2 = sizeof (buffer);
1751 hfi.hdr = extra->hdr;
1752 mutt_make_string_info (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt), &hfi, M_FORMAT_MAKEPRINT);
1754 else if (IsMsgAttach (extra))
1756 size_t l1 = COLS * MB_LEN_MAX;
1757 size_t l2 = sizeof (buffer);
1758 hfi.hdr = extra->bdy->hdr;
1759 mutt_make_string_info (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt), &hfi, M_FORMAT_MAKEPRINT);
1761 mutt_paddstr (COLS, IsHeader (extra) || IsMsgAttach (extra) ? buffer : banner);
1762 BKGDSET (MT_COLOR_NORMAL);
1763 SETCOLOR (MT_COLOR_NORMAL);
1766 if ((redraw & REDRAW_INDEX) && index)
1768 /* redraw the pager_index indicator, because the
1769 * flags for this message might have changed. */
1770 menu_redraw_current (index);
1772 /* print out the index status bar */
1773 menu_status_line (buffer, sizeof (buffer), index, NONULL(Status));
1775 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), 0);
1776 SETCOLOR (MT_COLOR_STATUS);
1777 BKGDSET (MT_COLOR_STATUS);
1778 mutt_paddstr (COLS, buffer);
1779 SETCOLOR (MT_COLOR_NORMAL);
1780 BKGDSET (MT_COLOR_NORMAL);
1785 if (option(OPTBRAILLEFRIENDLY)) {
1786 if (brailleLine!=-1) {
1787 move(brailleLine+1, 0);
1790 } else move (statusoffset, COLS-1);
1793 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1794 && lineInfo[curline].offset < sb.st_size-1)
1796 if (TopLine - topline > lines)
1805 ch = km_dokey (MENU_PAGER);
1807 mutt_clear_error ();
1815 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1818 mutt_resize_screen ();
1820 /* Store current position. */
1822 for (i = 0; i <= topline; i++)
1823 if (!lineInfo[i].continuation)
1826 if (flags & M_PAGER_RETWINCH)
1828 Resize = safe_malloc (sizeof (struct resize));
1830 Resize->line = lines;
1831 Resize->SearchCompiled = SearchCompiled;
1832 Resize->SearchBack = SearchBack;
1835 rc = OP_REFORMAT_WINCH;
1839 for (i = 0; i < maxLine; i++)
1841 lineInfo[i].offset = 0;
1842 lineInfo[i].type = -1;
1843 lineInfo[i].continuation = 0;
1844 lineInfo[i].chunks = 0;
1845 lineInfo[i].search_cnt = -1;
1846 lineInfo[i].quote = NULL;
1848 safe_realloc (&(lineInfo[i].syntax),
1849 sizeof (struct syntax_t));
1850 if (SearchCompiled && lineInfo[i].search)
1851 FREE (&(lineInfo[i].search));
1857 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1862 clearok(stdscr,TRUE);/*force complete redraw*/
1882 if (lineInfo[curline].offset < sb.st_size-1)
1884 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1886 else if (option (OPTPAGERSTOP))
1888 /* emulate "less -q" and don't go on to the next message. */
1889 mutt_error _("Bottom of message is shown.");
1893 /* end of the current message, so display the next message. */
1894 rc = OP_MAIN_NEXT_UNDELETED;
1902 topline = upNLines (bodylen-PagerContext, lineInfo, topline, hideQuoted);
1905 mutt_error _("Top of message is shown.");
1909 if (lineInfo[curline].offset < sb.st_size-1)
1914 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1920 mutt_error _("Bottom of message is shown.");
1925 topline = upNLines (1, lineInfo, topline, hideQuoted);
1927 mutt_error _("Top of message is shown.");
1934 mutt_error _("Top of message is shown.");
1939 topline = upNLines (bodylen/2, lineInfo, topline, hideQuoted);
1941 mutt_error _("Top of message is shown.");
1945 if (lineInfo[curline].offset < sb.st_size-1)
1947 topline = upNLines (bodylen/2, lineInfo, curline, hideQuoted);
1949 else if (option (OPTPAGERSTOP))
1951 /* emulate "less -q" and don't go on to the next message. */
1952 mutt_error _("Bottom of message is shown.");
1956 /* end of the current message, so display the next message. */
1957 rc = OP_MAIN_NEXT_UNDELETED;
1962 case OP_SEARCH_NEXT:
1963 case OP_SEARCH_OPPOSITE:
1969 if ((!SearchBack && ch==OP_SEARCH_NEXT) ||
1970 (SearchBack &&ch==OP_SEARCH_OPPOSITE))
1972 /* searching forward */
1973 for (i = wrapped ? 0 : topline + 1; i < lastLine; i++)
1975 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1976 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1982 else if (wrapped || !option (OPTWRAPSEARCH))
1983 mutt_error _("Not found.");
1986 mutt_message _("Search wrapped to top.");
1993 /* searching backward */
1994 for (i = wrapped ? lastLine : topline - 1; i >= 0; i--)
1996 if ((!hideQuoted || (has_types &&
1997 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1998 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2004 else if (wrapped || !option (OPTWRAPSEARCH))
2005 mutt_error _("Not found.");
2008 mutt_message _("Search wrapped to bottom.");
2014 if (lineInfo[topline].search_cnt > 0)
2015 SearchFlag = M_SEARCH;
2019 /* no previous search pattern, so fall through to search */
2022 case OP_SEARCH_REVERSE:
2023 strfcpy (buffer, searchbuf, sizeof (buffer));
2024 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
2025 _("Search: ")), buffer, sizeof (buffer),
2029 if (!strcmp (buffer, searchbuf))
2033 /* do an implicit search-next */
2034 if (ch == OP_SEARCH)
2035 ch = OP_SEARCH_NEXT;
2037 ch = OP_SEARCH_OPPOSITE;
2047 strfcpy (searchbuf, buffer, sizeof (searchbuf));
2049 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
2050 if (ch == OP_SEARCH)
2052 else if (ch == OP_SEARCH_REVERSE)
2057 regfree (&SearchRE);
2058 for (i = 0; i < lastLine; i++)
2060 if (lineInfo[i].search)
2061 FREE (&(lineInfo[i].search));
2062 lineInfo[i].search_cnt = -1;
2066 if ((err = REGCOMP (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf))) != 0)
2068 regerror (err, &SearchRE, buffer, sizeof (buffer));
2069 mutt_error ("%s", buffer);
2070 regfree (&SearchRE);
2071 for (i = 0; i < maxLine ; i++)
2074 if (lineInfo[i].search)
2075 FREE (&(lineInfo[i].search));
2076 lineInfo[i].search_cnt = -1;
2084 /* update the search pointers */
2086 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2087 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP) | (flags & M_PAGER_NOWRAP),
2088 &QuoteList, &q_level,
2089 &force_redraw, &SearchRE) == 0)
2094 /* searching forward */
2095 for (i = topline; i < lastLine; i++)
2097 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2098 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2102 if (i < lastLine) topline = i;
2106 /* searching backward */
2107 for (i = topline; i >= 0; i--)
2109 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2110 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2114 if (i >= 0) topline = i;
2117 if (lineInfo[topline].search_cnt == 0)
2120 mutt_error _("Not found.");
2123 SearchFlag = M_SEARCH;
2125 redraw = REDRAW_BODY;
2128 case OP_SEARCH_TOGGLE:
2131 SearchFlag ^= M_SEARCH;
2132 redraw = REDRAW_BODY;
2137 /* don't let the user enter the help-menu from the help screen! */
2141 mutt_help (MENU_PAGER);
2142 redraw = REDRAW_FULL;
2146 mutt_error _("Help is currently being shown.");
2149 case OP_PAGER_HIDE_QUOTED:
2152 hideQuoted ^= M_HIDE;
2153 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
2154 topline = upNLines (1, lineInfo, topline, hideQuoted);
2156 redraw = REDRAW_BODY;
2160 case OP_PAGER_SKIP_QUOTED:
2164 int new_topline = topline;
2166 while ((new_topline < lastLine ||
2167 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2168 new_topline, &lastLine, &maxLine, M_TYPES | (flags & M_PAGER_NOWRAP),
2169 &QuoteList, &q_level, &force_redraw, &SearchRE))))
2170 && lineInfo[new_topline].type != MT_COLOR_QUOTED)
2175 mutt_error _("No more quoted text.");
2179 while ((new_topline < lastLine ||
2180 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2181 new_topline, &lastLine, &maxLine, M_TYPES | (flags & M_PAGER_NOWRAP),
2182 &QuoteList, &q_level, &force_redraw, &SearchRE))))
2183 && lineInfo[new_topline].type == MT_COLOR_QUOTED)
2188 mutt_error _("No more unquoted text after quoted text.");
2191 topline = new_topline;
2195 case OP_PAGER_BOTTOM: /* move to the end of the file */
2196 if (lineInfo[curline].offset < sb.st_size - 1)
2199 /* make sure the types are defined to the end of file */
2200 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2201 &maxLine, has_types | (flags & M_PAGER_NOWRAP),
2202 &QuoteList, &q_level, &force_redraw,
2205 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2208 mutt_error _("Bottom of message is shown.");
2212 clearok (stdscr, TRUE);
2213 redraw = REDRAW_FULL;
2217 km_error_key (MENU_PAGER);
2220 /* --------------------------------------------------------------------
2221 * The following are operations on the current message rather than
2222 * adjusting the view of the message.
2225 case OP_BOUNCE_MESSAGE:
2226 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra))
2228 if (IsMsgAttach (extra))
2229 mutt_attach_bounce (extra->fp, extra->hdr,
2230 extra->idx, extra->idxlen,
2233 ci_bounce_message (extra->hdr, &redraw);
2237 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra))
2239 if (IsMsgAttach (extra))
2240 mutt_attach_resend (extra->fp, extra->hdr,
2241 extra->idx, extra->idxlen,
2244 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2245 redraw = REDRAW_FULL;
2248 case OP_CHECK_TRADITIONAL:
2249 CHECK_MODE (IsHeader (extra));
2250 if (!(WithCrypto & APPLICATION_PGP))
2252 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED))
2255 rc = OP_CHECK_TRADITIONAL;
2259 case OP_CREATE_ALIAS:
2260 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2261 if (IsMsgAttach (extra))
2262 mutt_create_alias (extra->bdy->hdr->env, NULL);
2264 mutt_create_alias (extra->hdr->env, NULL);
2265 MAYBE_REDRAW (redraw);
2269 CHECK_MODE(IsHeader (extra));
2271 CHECK_ACL(M_ACL_DELETE, _("delete message"));
2273 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2274 if (option (OPTDELETEUNTAG))
2275 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2276 redraw = REDRAW_STATUS | REDRAW_INDEX;
2277 if (option (OPTRESOLVE))
2280 rc = OP_MAIN_NEXT_UNDELETED;
2284 case OP_DELETE_THREAD:
2285 case OP_DELETE_SUBTHREAD:
2286 CHECK_MODE(IsHeader (extra));
2288 CHECK_ACL(M_ACL_DELETE, _("delete message(s)"));
2290 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2291 ch == OP_DELETE_THREAD ? 0 : 1);
2295 if (option (OPTDELETEUNTAG))
2296 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2297 ch == OP_DELETE_THREAD ? 0 : 1);
2298 if (option (OPTRESOLVE))
2300 rc = OP_MAIN_NEXT_UNDELETED;
2304 if (!option (OPTRESOLVE) && PagerIndexLines)
2305 redraw = REDRAW_FULL;
2307 redraw = REDRAW_STATUS | REDRAW_INDEX;
2311 case OP_DISPLAY_ADDRESS:
2312 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2313 if (IsMsgAttach (extra))
2314 mutt_display_address (extra->bdy->hdr->env);
2316 mutt_display_address (extra->hdr->env);
2319 case OP_ENTER_COMMAND:
2320 old_smart_wrap = option (OPTWRAP);
2321 old_markers = option (OPTMARKERS);
2322 old_PagerIndexLines = PagerIndexLines;
2324 CurrentMenu = MENU_PAGER;
2325 mutt_enter_command ();
2327 if (option (OPTNEEDRESORT))
2329 unset_option (OPTNEEDRESORT);
2330 CHECK_MODE(IsHeader (extra));
2331 set_option (OPTNEEDRESORT);
2334 if (old_PagerIndexLines != PagerIndexLines)
2337 mutt_menuDestroy (&index);
2341 if (option (OPTWRAP) != old_smart_wrap ||
2342 option (OPTMARKERS) != old_markers)
2344 if (flags & M_PAGER_RETWINCH)
2347 rc = OP_REFORMAT_WINCH;
2351 /* count the real lines above */
2353 for (i = 0; i <= topline; i++)
2355 if (!lineInfo[i].continuation)
2359 /* we need to restart the whole thing */
2360 for (i = 0; i < maxLine; i++)
2362 lineInfo[i].offset = 0;
2363 lineInfo[i].type = -1;
2364 lineInfo[i].continuation = 0;
2365 lineInfo[i].chunks = 0;
2366 lineInfo[i].search_cnt = -1;
2367 lineInfo[i].quote = NULL;
2369 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2370 if (SearchCompiled && lineInfo[i].search)
2371 FREE (&(lineInfo[i].search));
2376 regfree (&SearchRE);
2381 /* try to keep the old position */
2384 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2385 &lastLine, &maxLine,
2386 (has_types ? M_TYPES : 0) | (flags & M_PAGER_NOWRAP),
2387 &QuoteList, &q_level, &force_redraw,
2390 if (! lineInfo[topline].continuation)
2399 if (option (OPTFORCEREDRAWPAGER))
2400 redraw = REDRAW_FULL;
2401 unset_option (OPTFORCEREDRAWINDEX);
2402 unset_option (OPTFORCEREDRAWPAGER);
2405 case OP_FLAG_MESSAGE:
2406 CHECK_MODE(IsHeader (extra));
2408 CHECK_ACL(M_ACL_WRITE, "flag message");
2410 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2411 redraw = REDRAW_STATUS | REDRAW_INDEX;
2412 if (option (OPTRESOLVE))
2415 rc = OP_MAIN_NEXT_UNDELETED;
2420 CHECK_MODE(IsHeader (extra) || IsAttach (extra));
2421 if (IsAttach (extra))
2422 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2424 mutt_pipe_message (extra->hdr);
2425 MAYBE_REDRAW (redraw);
2429 CHECK_MODE(IsHeader (extra) || IsAttach (extra));
2430 if (IsAttach (extra))
2431 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2433 mutt_print_message (extra->hdr);
2437 CHECK_MODE(IsHeader (extra) && !IsAttach (extra));
2439 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2440 redraw = REDRAW_FULL;
2444 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2446 if (IsMsgAttach (extra))
2447 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2448 extra->idxlen, extra->bdy,
2451 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2452 redraw = REDRAW_FULL;
2455 case OP_RECALL_MESSAGE:
2456 CHECK_MODE(IsHeader (extra) && !IsAttach(extra));
2458 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2459 redraw = REDRAW_FULL;
2462 case OP_GROUP_REPLY:
2463 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2465 if (IsMsgAttach (extra))
2466 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2467 extra->idxlen, extra->bdy, SENDREPLY|SENDGROUPREPLY);
2469 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx, extra->hdr);
2470 redraw = REDRAW_FULL;
2474 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2476 if (IsMsgAttach (extra))
2477 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2478 extra->idxlen, extra->bdy, SENDREPLY|SENDLISTREPLY);
2480 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx, extra->hdr);
2481 redraw = REDRAW_FULL;
2484 case OP_FORWARD_MESSAGE:
2485 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2487 if (IsMsgAttach (extra))
2488 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2489 extra->idxlen, extra->bdy);
2491 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2492 redraw = REDRAW_FULL;
2495 case OP_DECRYPT_SAVE:
2503 if (IsAttach (extra))
2505 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr, NULL);
2509 case OP_COPY_MESSAGE:
2510 case OP_DECODE_SAVE:
2511 case OP_DECODE_COPY:
2512 case OP_DECRYPT_COPY:
2513 if (!WithCrypto && ch == OP_DECRYPT_COPY)
2518 CHECK_MODE(IsHeader (extra));
2519 if (mutt_save_message (extra->hdr,
2520 (ch == OP_DECRYPT_SAVE) ||
2521 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2522 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2523 (ch == OP_DECRYPT_SAVE) || (ch == OP_DECRYPT_COPY) ||
2525 &redraw) == 0 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2526 || ch == OP_DECRYPT_SAVE
2529 if (option (OPTRESOLVE))
2532 rc = OP_MAIN_NEXT_UNDELETED;
2535 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2537 MAYBE_REDRAW (redraw);
2540 case OP_SHELL_ESCAPE:
2541 mutt_shell_escape ();
2542 MAYBE_REDRAW (redraw);
2546 CHECK_MODE(IsHeader (extra));
2547 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2549 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2550 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2551 ? NULL : Context->last_tag);
2553 redraw = REDRAW_STATUS | REDRAW_INDEX;
2554 if (option (OPTRESOLVE))
2562 CHECK_MODE(IsHeader (extra));
2564 CHECK_ACL(M_ACL_SEEN, _("toggle new"));
2566 if (extra->hdr->read || extra->hdr->old)
2567 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2569 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2571 Context->msgnotreadyet = -1;
2572 redraw = REDRAW_STATUS | REDRAW_INDEX;
2573 if (option (OPTRESOLVE))
2576 rc = OP_MAIN_NEXT_UNDELETED;
2581 CHECK_MODE(IsHeader (extra));
2583 CHECK_ACL(M_ACL_DELETE, _("undelete message"));
2585 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2586 redraw = REDRAW_STATUS | REDRAW_INDEX;
2587 if (option (OPTRESOLVE))
2594 case OP_UNDELETE_THREAD:
2595 case OP_UNDELETE_SUBTHREAD:
2596 CHECK_MODE(IsHeader (extra));
2598 CHECK_ACL(M_ACL_DELETE, _("undelete message(s)"));
2600 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2601 ch == OP_UNDELETE_THREAD ? 0 : 1);
2605 if (option (OPTRESOLVE))
2607 rc = (ch == OP_DELETE_THREAD) ?
2608 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2612 if (!option (OPTRESOLVE) && PagerIndexLines)
2613 redraw = REDRAW_FULL;
2615 redraw = REDRAW_STATUS | REDRAW_INDEX;
2627 case OP_VIEW_ATTACHMENTS:
2628 if (flags & M_PAGER_ATTACHMENT)
2631 rc = OP_ATTACH_COLLAPSE;
2634 CHECK_MODE(IsHeader (extra));
2635 mutt_view_attachments (extra->hdr);
2636 if (extra->hdr->attach_del)
2637 Context->changed = 1;
2638 redraw = REDRAW_FULL;
2643 if (!(WithCrypto & APPLICATION_PGP))
2648 CHECK_MODE(IsHeader(extra));
2650 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2651 redraw = REDRAW_FULL;
2655 case OP_FORGET_PASSPHRASE:
2656 crypt_forget_passphrase ();
2659 case OP_EXTRACT_KEYS:
2665 CHECK_MODE(IsHeader(extra));
2666 crypt_extract_keys_from_messages(extra->hdr);
2667 redraw = REDRAW_FULL;
2681 if (IsHeader (extra))
2683 Context->msgnotreadyet = -1;
2689 OldHdr = extra->hdr;
2693 cleanup_quote (&QuoteList);
2695 for (i = 0; i < maxLine ; i++)
2697 FREE (&(lineInfo[i].syntax));
2698 if (SearchCompiled && lineInfo[i].search)
2699 FREE (&(lineInfo[i].search));
2703 regfree (&SearchRE);
2708 mutt_menuDestroy(&index);
2709 return (rc != -1 ? rc : 0);