2 * Copyright (C) 1996-2002 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 #include "mutt_curses.h"
25 #include "mutt_regex.h"
27 #include "mutt_menu.h"
38 #include "imap_private.h"
41 #include "mutt_crypt.h"
50 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
52 #define IsAttach(x) (x && (x)->bdy)
53 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
54 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
55 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
56 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
58 static const char *Not_available_in_this_menu =
59 N_("Not available in this menu.");
60 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
61 static const char *Function_not_permitted_in_attach_message_mode =
62 N_("Function not permitted in attach-message mode.");
64 #define CHECK_MODE(x) if (!(x)) \
67 mutt_error _(Not_available_in_this_menu); \
71 #define CHECK_READONLY if (Context->readonly) \
74 mutt_error _(Mailbox_is_read_only); \
78 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
81 mutt_error _(Function_not_permitted_in_attach_message_mode); \
86 /* the error message returned here could be better. */
87 #define CHECK_IMAP_ACL(aclbit) if (Context->magic == M_IMAP) \
88 if (mutt_bit_isset (((IMAP_DATA *)Context->data)->capabilities, ACL) \
89 && !mutt_bit_isset(((IMAP_DATA *)Context->data)->rights,aclbit)){ \
91 mutt_error ("Operation not permitted by the IMAP ACL for this mailbox"); \
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 FREE (&(*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 *) safe_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 (mutt_strncmp (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 *) safe_calloc (1, sizeof (struct q_class_t));
403 tmp->prefix = (char *) safe_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 && mutt_strncmp (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 (mutt_strncmp (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 *) safe_calloc (1,
507 tmp->prefix = (char *) safe_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 && mutt_strncmp (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 *) safe_calloc (1, sizeof (struct q_class_t));
603 tmp->prefix = (char *) safe_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 *) safe_calloc (1, sizeof (struct q_class_t));
636 class->prefix = (char *) safe_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 check_attachment_marker (char *);
657 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
658 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
661 COLOR_LINE *color_line;
662 regmatch_t pmatch[1], smatch[1];
663 int found, offset, null_rx, i;
665 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
667 lineInfo[n].type = MT_COLOR_NORMAL;
668 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
669 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
670 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
673 lineInfo[n].type = MT_COLOR_HDEFAULT;
674 color_line = ColorHdrList;
676 if (REGEXEC (color_line->rx, buf) == 0) {
677 lineInfo[n].type = MT_COLOR_HEADER;
678 lineInfo[n].syntax[0].color = color_line->pair;
681 color_line = color_line->next;
685 else if (mutt_strncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
686 lineInfo[n].type = MT_COLOR_NORMAL;
688 else if (mutt_strncmp ("[-- ", buf, 4) == 0)
689 lineInfo[n].type = MT_COLOR_ATTACHMENT;
691 else if (check_attachment_marker ((char *) raw) == 0)
692 lineInfo[n].type = MT_COLOR_ATTACHMENT;
694 else if (mutt_strcmp ("-- \n", buf) == 0
695 || mutt_strcmp ("-- \r\n", buf) == 0) {
698 lineInfo[n].type = MT_COLOR_SIGNATURE;
699 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
700 (lineInfo[i].type == MT_COLOR_NORMAL ||
701 lineInfo[i].type == MT_COLOR_QUOTED ||
702 lineInfo[i].type == MT_COLOR_HEADER)) {
704 if (lineInfo[i].chunks) {
705 lineInfo[i].chunks = 0;
706 safe_realloc (&(lineInfo[n].syntax), sizeof (struct syntax_t));
708 lineInfo[i++].type = MT_COLOR_SIGNATURE;
711 else if (check_sig (buf, lineInfo, n - 1) == 0)
712 lineInfo[n].type = MT_COLOR_SIGNATURE;
713 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
714 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
715 if (smatch[0].rm_so > 0) {
718 /* hack to avoid making an extra copy of buf */
719 c = buf[smatch[0].rm_so];
720 buf[smatch[0].rm_so] = 0;
722 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
723 if (q_classify && lineInfo[n].quote == NULL)
724 lineInfo[n].quote = classify_quote (QuoteList,
725 buf + pmatch[0].rm_so,
727 pmatch[0].rm_so, force_redraw,
729 lineInfo[n].type = MT_COLOR_QUOTED;
732 lineInfo[n].type = MT_COLOR_NORMAL;
734 buf[smatch[0].rm_so] = c;
737 lineInfo[n].type = MT_COLOR_NORMAL;
740 if (q_classify && lineInfo[n].quote == NULL)
741 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
742 pmatch[0].rm_eo - pmatch[0].rm_so,
743 force_redraw, q_level);
744 lineInfo[n].type = MT_COLOR_QUOTED;
748 lineInfo[n].type = MT_COLOR_NORMAL;
751 if (lineInfo[n].type == MT_COLOR_NORMAL ||
752 lineInfo[n].type == MT_COLOR_QUOTED) {
756 lineInfo[n].chunks = 0;
763 color_line = ColorBodyList;
765 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
766 (offset ? REG_NOTBOL : 0)) == 0) {
767 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
769 if (++(lineInfo[n].chunks) > 1)
770 safe_realloc (&(lineInfo[n].syntax),
771 (lineInfo[n].chunks) *
772 sizeof (struct syntax_t));
774 i = lineInfo[n].chunks - 1;
775 pmatch[0].rm_so += offset;
776 pmatch[0].rm_eo += offset;
778 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
779 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
780 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
781 (lineInfo[n].syntax)[i].color = color_line->pair;
782 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
783 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
789 null_rx = 1; /* empty regexp; don't add it, but keep looking */
791 color_line = color_line->next;
795 offset++; /* avoid degenerate cases */
797 offset = (lineInfo[n].syntax)[i].last;
798 } while (found || null_rx);
802 static int is_ansi (unsigned char *buf)
804 while (*buf && (isdigit (*buf) || *buf == ';'))
806 return (*buf == 'm');
809 static int check_attachment_marker (char *p)
811 char *q = AttachmentMarker;
813 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
814 return (int) (*p - *q);
817 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
821 while (isdigit (buf[x]) || buf[x] == ';')
824 /* Character Attributes */
825 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
829 mutt_free_color (a->fg, a->bg);
835 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
836 a->attr |= ANSI_BOLD;
839 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
840 a->attr |= ANSI_UNDERLINE;
843 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
844 a->attr |= ANSI_BLINK;
847 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
848 a->attr |= ANSI_REVERSE;
851 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
854 mutt_free_color (a->fg, a->bg);
860 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
863 mutt_free_color (a->fg, a->bg);
866 a->attr |= ANSI_COLOR;
867 a->fg = buf[pos + 1] - '0';
870 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
873 mutt_free_color (a->fg, a->bg);
876 a->attr |= ANSI_COLOR;
877 a->bg = buf[pos + 1] - '0';
881 while (pos < x && buf[pos] != ';')
892 fill_buffer (FILE * f, long *last_pos, long offset, unsigned char *buf,
893 unsigned char *fmt, size_t blen, int *buf_ready)
896 static int b_read = 0;
898 if (*buf_ready == 0) {
900 if (offset != *last_pos)
901 fseek (f, offset, 0);
902 if (fgets ((char *) buf, blen - 1, f) == NULL) {
906 *last_pos = ftell (f);
907 b_read = (int) (*last_pos - offset);
910 /* copy "buf" to "fmt", but without bold and underline controls */
913 if (*p == '\010' && (p > buf)) {
914 if (*(p + 1) == '_') /* underline */
916 else if (*(p + 1)) { /* bold or overstrike */
917 *(fmt - 1) = *(p + 1);
923 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
924 while (*p++ != 'm') /* skip ANSI sequence */
927 else if (*p == '\033' && *(p + 1) == ']'
928 && check_attachment_marker ((char *) p) == 0) {
929 dprint (2, (debugfile, "fill_buffer: Seen attachment marker.\n"));
930 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
947 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
948 int flags, ansi_attr * pa, int cnt,
949 int *pspace, int *pvch, int *pcol, int *pspecial)
951 int space = -1; /* index of the last space or TAB */
952 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
953 int ch, vch, k, last_special = -1, special = 0, t;
957 int wrap_cols = COLS;
959 if (!(flags & (M_SHOWFLAT)))
960 wrap_cols -= WrapMargin;
961 wrap_cols -= SidebarWidth;
966 /* FIXME: this should come from lineInfo */
967 memset (&mbstate, 0, sizeof (mbstate));
969 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
970 /* Handle ANSI sequences */
971 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
972 is_ansi (buf + ch + 2))
973 ch = grok_ansi (buf, ch + 2, pa) + 1;
975 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
976 check_attachment_marker ((char *) buf + ch) == 0) {
977 while (buf[ch++] != '\a')
982 /* is anything left to do? */
986 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
987 if (k == -2 || k == -1) {
988 dprint (1, (debugfile, "%s:%d: mbrtowc returned %d; errno = %d.\n",
989 __FILE__, __LINE__, k, errno));
990 if (col + 4 > wrap_cols)
994 printw ("\\%03o", buf[ch]);
1001 /* Handle backspace */
1003 if (IsWPrint (wc)) {
1008 while ((wc1 = 0, mbstate1 = mbstate,
1010 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
1011 &mbstate1), k1 - k > 0 && wc1 == '\b')
1013 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1014 &mbstate1), k2 > 0 && IsWPrint (wc1))) {
1016 special |= (wc == '_' && special & A_UNDERLINE)
1017 ? A_UNDERLINE : A_BOLD;
1019 else if (wc == '_' || wc1 == '_') {
1020 special |= A_UNDERLINE;
1021 wc = (wc1 == '_') ? wc : wc1;
1024 /* special = 0; / * overstrike: nothing to do! */
1034 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1035 special || last_special || pa->attr)) {
1036 resolve_color (*lineInfo, n, vch, flags, special, pa);
1037 last_special = special;
1040 if (IsWPrint (wc)) {
1044 if (col + t > wrap_cols)
1050 else if (wc == '\n')
1052 else if (wc == '\t') {
1058 for (; col < t; col++)
1063 else if (wc < 0x20 || wc == 0x7f) {
1064 if (col + 2 > wrap_cols)
1068 printw ("^%c", ('@' + wc) & 0x7f);
1070 else if (wc < 0x100) {
1071 if (col + 4 > wrap_cols)
1075 printw ("\\%03o", wc);
1078 if (col + 1 > wrap_cols)
1082 addch (replacement_char ());
1088 *pspecial = special;
1094 * flags M_SHOWFLAT, show characters (used for displaying help)
1095 * M_SHOWCOLOR, show characters in color
1096 * otherwise don't show characters
1097 * M_HIDE, don't show quoted text
1098 * M_SEARCH, resolve search patterns
1099 * M_TYPES, compute line's type
1100 * M_PAGER_NSKIP, keeps leading whitespace
1101 * M_PAGER_MARKER, eventually show markers
1104 * -1 EOF was reached
1105 * 0 normal exit, line was not displayed
1106 * >0 normal exit, line was displayed
1110 display_line (FILE * f, long *last_pos, struct line_t **lineInfo, int n,
1111 int *last, int *max, int flags, struct q_class_t **QuoteList,
1112 int *q_level, int *force_redraw, regex_t * SearchRE)
1114 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1115 unsigned char *buf_ptr = buf;
1116 int ch, vch, col, cnt, b_read;
1117 int buf_ready = 0, change_last = 0;
1122 ansi_attr a = { 0, 0, 0, -1 };
1123 regmatch_t pmatch[1];
1130 if (*last == *max) {
1131 safe_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1132 for (ch = *last; ch < *max; ch++) {
1133 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1134 (*lineInfo)[ch].type = -1;
1135 (*lineInfo)[ch].search_cnt = -1;
1136 (*lineInfo)[ch].syntax = safe_malloc (sizeof (struct syntax_t));
1137 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1142 /* only do color hiliting if we are viewing a message */
1143 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1144 if ((*lineInfo)[n].type == -1) {
1145 /* determine the line class */
1147 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1154 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1155 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1157 /* avoid race condition for continuation lines when scrolling up */
1159 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1161 (*lineInfo)[m].type = (*lineInfo)[n].type;
1164 /* this also prevents searching through the hidden lines */
1165 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1166 flags = 0; /* M_NOSHOW */
1169 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1170 * don't want to compute it every time M_TYPES is set, since this
1171 * would slow down the "bottom" function unacceptably. A compromise
1172 * solution is hence to call regexec() again, just to find out the
1173 * length of the quote prefix.
1175 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1176 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1179 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1185 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1186 (*lineInfo)[n].quote = classify_quote (QuoteList,
1187 (char *) fmt + pmatch[0].rm_so,
1188 pmatch[0].rm_eo - pmatch[0].rm_so,
1189 force_redraw, q_level);
1192 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1193 && (*lineInfo)[n].search_cnt == -1) {
1195 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1203 (*lineInfo)[n].search_cnt = 0;
1205 (SearchRE, (char *) fmt + offset, 1, pmatch,
1206 (offset ? REG_NOTBOL : 0)) == 0) {
1207 if (++((*lineInfo)[n].search_cnt) > 1)
1208 safe_realloc (&((*lineInfo)[n].search),
1209 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1211 (*lineInfo)[n].search = safe_malloc (sizeof (struct syntax_t));
1212 pmatch[0].rm_so += offset;
1213 pmatch[0].rm_eo += offset;
1214 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1216 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1219 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1220 offset++; /* avoid degenerate cases */
1222 offset = pmatch[0].rm_eo;
1228 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1229 /* we've already scanned this line, so just exit */
1232 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1233 /* no need to try to display this line... */
1234 return (1); /* fake display */
1237 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1238 sizeof (buf), &buf_ready)) < 0) {
1244 /* now chose a good place to break the line */
1246 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1248 buf_ptr = buf + cnt;
1250 /* move the break point only if smart_wrap is set */
1251 if (option (OPTWRAP)) {
1253 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1254 && buf[cnt] != '\r') {
1256 /* skip trailing blanks */
1257 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1262 buf_ptr = buf + cnt; /* a very long word... */
1264 if (!(flags & M_PAGER_NSKIP))
1265 /* skip leading blanks on the next line too */
1266 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1270 if (*buf_ptr == '\r')
1272 if (*buf_ptr == '\n')
1275 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1276 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1277 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1279 /* if we don't need to display the line we are done */
1280 if (!(flags & M_SHOW))
1283 /* display the line */
1284 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1286 /* avoid a bug in ncurses... */
1287 #ifndef USE_SLANG_CURSES
1289 SETCOLOR (MT_COLOR_NORMAL);
1294 /* end the last color pattern (needed by S-Lang) */
1295 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1296 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1299 * Fill the blank space at the end of the line with the prevailing color.
1300 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1301 * to make sure to reset the color *after* that
1303 if (flags & M_SHOWCOLOR) {
1304 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1305 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1306 def_color = ((*lineInfo)[m].syntax)[0].color;
1308 def_color = ColorDefs[(*lineInfo)[m].type];
1310 attrset (def_color);
1312 bkgdset (def_color | ' ');
1316 /* ncurses always wraps lines when you get to the right side of the
1317 * screen, but S-Lang seems to only wrap if the next character is *not*
1320 #ifndef USE_SLANG_CURSES
1326 * reset the color back to normal. This *must* come after the
1327 * addch('\n'), otherwise the color for this line will not be
1328 * filled to the right margin.
1330 if (flags & M_SHOWCOLOR) {
1331 SETCOLOR (MT_COLOR_NORMAL);
1332 BKGDSET (MT_COLOR_NORMAL);
1335 /* build a return code */
1336 if (!(flags & M_SHOW))
1342 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1344 while (cur > 0 && nlines > 0) {
1346 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1353 static struct mapping_t PagerHelp[] = {
1354 {N_("Exit"), OP_EXIT},
1355 {N_("PrevPg"), OP_PREV_PAGE},
1356 {N_("NextPg"), OP_NEXT_PAGE},
1359 static struct mapping_t PagerHelpExtra[] = {
1360 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1361 {N_("Del"), OP_DELETE},
1362 {N_("Reply"), OP_REPLY},
1363 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1368 static struct mapping_t PagerNewsHelpExtra[] = {
1369 {N_("Post"), OP_POST},
1370 {N_("Followup"), OP_FOLLOWUP},
1371 {N_("Del"), OP_DELETE},
1372 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1379 /* This pager is actually not so simple as it once was. It now operates in
1380 two modes: one for viewing messages and the other for viewing help. These
1381 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1382 is there so that we can do operations on the current message without the
1383 need to pop back out to the main-menu. */
1385 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1387 static char searchbuf[STRING];
1388 char buffer[LONG_STRING];
1389 char helpstr[SHORT_STRING * 2];
1390 char tmphelp[SHORT_STRING * 2];
1391 int maxLine, lastLine = 0;
1392 struct line_t *lineInfo;
1393 struct q_class_t *QuoteList = NULL;
1394 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1395 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1397 int redraw = REDRAW_FULL;
1399 long last_pos = 0, last_offset = 0;
1400 int old_smart_wrap, old_markers;
1403 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1404 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1406 int bodyoffset = 1; /* offset of first line of real text */
1407 int statusoffset = 0; /* offset for the status bar */
1408 int helpoffset = LINES - 2; /* offset for the help bar. */
1409 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1411 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1412 int indexoffset = 0; /* offset for the PI */
1413 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1414 int indicator = indexlen / 3; /* the indicator line of the PI */
1415 int old_PagerIndexLines; /* some people want to resize it
1416 * while inside the pager... */
1422 if (!(flags & M_SHOWCOLOR))
1423 flags |= M_SHOWFLAT;
1425 if ((fp = fopen (fname, "r")) == NULL) {
1426 mutt_perror (fname);
1430 if (stat (fname, &sb) != 0) {
1431 mutt_perror (fname);
1437 /* Initialize variables */
1439 if (IsHeader (extra) && !extra->hdr->read) {
1440 Context->msgnotreadyet = extra->hdr->msgno;
1441 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1444 lineInfo = safe_malloc (sizeof (struct line_t) * (maxLine = LINES));
1445 for (i = 0; i < maxLine; i++) {
1446 memset (&lineInfo[i], 0, sizeof (struct line_t));
1447 lineInfo[i].type = -1;
1448 lineInfo[i].search_cnt = -1;
1449 lineInfo[i].syntax = safe_malloc (sizeof (struct syntax_t));
1450 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1453 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1454 if (IsHeader (extra)) {
1455 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1456 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1459 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1462 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1465 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1466 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1467 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1477 if (redraw & REDRAW_FULL) {
1478 SETCOLOR (MT_COLOR_NORMAL);
1479 /* clear() doesn't optimize screen redraws */
1483 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1484 indexlen = Context->vcount + 1;
1486 indexlen = PagerIndexLines;
1488 indicator = indexlen / 3;
1490 if (option (OPTSTATUSONTOP)) {
1492 statusoffset = IsHeader (extra) ? indexlen : 0;
1493 bodyoffset = statusoffset + 1;
1494 helpoffset = LINES - 2;
1495 bodylen = helpoffset - bodyoffset;
1496 if (!option (OPTHELP))
1502 statusoffset = LINES - 2;
1503 if (!option (OPTHELP))
1505 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1506 bodylen = statusoffset - bodyoffset;
1509 if (option (OPTHELP)) {
1510 SETCOLOR (MT_COLOR_STATUS);
1511 move (helpoffset, 0);
1512 mutt_paddstr (COLS, helpstr);
1513 SETCOLOR (MT_COLOR_NORMAL);
1516 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1517 if (Resize != NULL) {
1518 if ((SearchCompiled = Resize->SearchCompiled)) {
1520 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1521 SearchFlag = M_SEARCH;
1522 SearchBack = Resize->SearchBack;
1524 lines = Resize->line;
1525 redraw |= REDRAW_SIGWINCH;
1531 if (IsHeader (extra) && PagerIndexLines) {
1532 if (index == NULL) {
1533 /* only allocate the space if/when we need the index.
1534 Initialise the menu as per the main index */
1535 index = mutt_new_menu ();
1536 index->menu = MENU_MAIN;
1537 index->make_entry = index_make_entry;
1538 index->color = index_color;
1539 index->max = Context->vcount;
1540 index->current = extra->hdr->virtual;
1543 SETCOLOR (MT_COLOR_NORMAL);
1544 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1546 index->pagelen = indexlen - 1;
1548 /* some fudge to work out where abouts the indicator should go */
1549 if (index->current - indicator < 0)
1551 else if (index->max - index->current < index->pagelen - indicator)
1552 index->top = index->max - index->pagelen;
1554 index->top = index->current - indicator;
1556 menu_redraw_index (index);
1559 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1563 if (redraw & REDRAW_SIGWINCH) {
1566 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1567 has_types | SearchFlag, &QuoteList, &q_level,
1568 &force_redraw, &SearchRE) == 0) {
1569 if (!lineInfo[i].continuation && ++j == lines) {
1574 redraw |= REDRAW_SIDEBAR;
1578 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1580 move (bodyoffset, SidebarWidth);
1581 curline = oldtopline = topline;
1585 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1586 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1588 (flags & M_DISPLAYFLAGS) | hideQuoted |
1589 SearchFlag, &QuoteList, &q_level, &force_redraw,
1593 move (lines + bodyoffset, SidebarWidth);
1594 redraw |= REDRAW_SIDEBAR;
1596 last_offset = lineInfo[curline].offset;
1597 } while (force_redraw);
1599 SETCOLOR (MT_COLOR_TILDE);
1600 BKGDSET (MT_COLOR_TILDE);
1601 while (lines < bodylen) {
1603 if (option (OPTTILDE))
1607 move (lines + bodyoffset, SidebarWidth);
1609 /* We are going to update the pager status bar, so it isn't
1610 * necessary to reset to normal color now. */
1612 redraw |= REDRAW_STATUS; /* need to update the % seen */
1615 if (redraw & REDRAW_STATUS) {
1616 /* print out the pager status bar */
1617 SETCOLOR (MT_COLOR_STATUS);
1618 BKGDSET (MT_COLOR_STATUS);
1619 CLEARLINE_WIN (statusoffset);
1620 if (IsHeader (extra)) {
1621 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1622 size_t l2 = sizeof (buffer);
1624 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1625 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1627 else if (IsMsgAttach (extra)) {
1628 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1629 size_t l2 = sizeof (buffer);
1631 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1632 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1634 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)),
1635 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);
1636 mutt_paddstr (COLS - 10, IsHeader (extra)
1637 || IsMsgAttach (extra) ? buffer : banner);
1640 if (last_pos < sb.st_size - 1)
1641 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1643 addstr (topline == 0 ? "all)" : "end)");
1644 BKGDSET (MT_COLOR_NORMAL);
1645 SETCOLOR (MT_COLOR_NORMAL);
1648 if (redraw & REDRAW_SIDEBAR)
1649 draw_sidebar (MENU_PAGER);
1651 if ((redraw & REDRAW_INDEX) && index) {
1652 /* redraw the pager_index indicator, because the
1653 * flags for this message might have changed. */
1654 menu_redraw_current (index);
1655 draw_sidebar (MENU_PAGER);
1656 /* print out the index status bar */
1657 menu_status_line (buffer, sizeof (buffer), index, NONULL (Status));
1658 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)),
1659 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);
1660 SETCOLOR (MT_COLOR_STATUS);
1661 mutt_paddstr (COLS - (option (OPTSTATUSONTOP) ? 0 : SidebarWidth),
1663 SETCOLOR (MT_COLOR_NORMAL);
1665 /* if we're not using the index, update every time */
1667 draw_sidebar (MENU_PAGER);
1671 move (statusoffset, COLS - 1);
1673 ch = km_dokey (MENU_PAGER);
1675 mutt_clear_error ();
1682 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1683 else if (SigWinch) {
1684 mutt_resize_screen ();
1686 /* Store current position. */
1688 for (i = 0; i <= topline; i++)
1689 if (!lineInfo[i].continuation)
1692 if (flags & M_PAGER_RETWINCH) {
1693 Resize = safe_malloc (sizeof (struct resize));
1695 Resize->line = lines;
1696 Resize->SearchCompiled = SearchCompiled;
1697 Resize->SearchBack = SearchBack;
1700 rc = OP_REFORMAT_WINCH;
1703 for (i = 0; i < maxLine; i++) {
1704 lineInfo[i].offset = 0;
1705 lineInfo[i].type = -1;
1706 lineInfo[i].continuation = 0;
1707 lineInfo[i].chunks = 0;
1708 lineInfo[i].search_cnt = -1;
1709 lineInfo[i].quote = NULL;
1711 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
1712 if (SearchCompiled && lineInfo[i].search)
1713 FREE (&(lineInfo[i].search));
1719 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1724 clearok (stdscr, TRUE); /*force complete redraw */
1728 else if (ch == -1) {
1742 if (lineInfo[curline].offset < sb.st_size - 1) {
1743 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1745 else if (option (OPTPAGERSTOP)) {
1746 /* emulate "less -q" and don't go on to the next message. */
1747 mutt_error _("Bottom of message is shown.");
1750 /* end of the current message, so display the next message. */
1751 rc = OP_MAIN_NEXT_UNDELETED;
1759 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1762 mutt_error _("Top of message is shown.");
1766 if (lineInfo[curline].offset < sb.st_size - 1) {
1769 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1775 mutt_error _("Bottom of message is shown.");
1780 topline = upNLines (1, lineInfo, topline, hideQuoted);
1782 mutt_error _("Top of message is shown.");
1789 mutt_error _("Top of message is shown.");
1794 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1796 mutt_error _("Top of message is shown.");
1800 if (lineInfo[curline].offset < sb.st_size - 1) {
1801 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1803 else if (option (OPTPAGERSTOP)) {
1804 /* emulate "less -q" and don't go on to the next message. */
1805 mutt_error _("Bottom of message is shown.");
1808 /* end of the current message, so display the next message. */
1809 rc = OP_MAIN_NEXT_UNDELETED;
1814 case OP_SEARCH_NEXT:
1815 case OP_SEARCH_OPPOSITE:
1816 if (SearchCompiled) {
1818 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1819 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1820 /* searching forward */
1821 for (i = topline + 1; i < lastLine; i++) {
1822 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1823 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1830 mutt_error _("Not found.");
1833 /* searching backward */
1834 for (i = topline - 1; i >= 0; i--) {
1835 if ((!hideQuoted || (has_types &&
1836 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1837 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1844 mutt_error _("Not found.");
1847 if (lineInfo[topline].search_cnt > 0)
1848 SearchFlag = M_SEARCH;
1852 /* no previous search pattern, so fall through to search */
1855 case OP_SEARCH_REVERSE:
1856 strfcpy (buffer, searchbuf, sizeof (buffer));
1857 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1858 _("Search: ")), buffer, sizeof (buffer),
1862 if (!strcmp (buffer, searchbuf)) {
1863 if (SearchCompiled) {
1864 /* do an implicit search-next */
1865 if (ch == OP_SEARCH)
1866 ch = OP_SEARCH_NEXT;
1868 ch = OP_SEARCH_OPPOSITE;
1877 strfcpy (searchbuf, buffer, sizeof (searchbuf));
1879 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1880 if (ch == OP_SEARCH)
1882 else if (ch == OP_SEARCH_REVERSE)
1885 if (SearchCompiled) {
1886 regfree (&SearchRE);
1887 for (i = 0; i < lastLine; i++) {
1888 if (lineInfo[i].search)
1889 FREE (&(lineInfo[i].search));
1890 lineInfo[i].search_cnt = -1;
1895 REGCOMP (&SearchRE, searchbuf,
1896 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1897 regerror (err, &SearchRE, buffer, sizeof (buffer));
1898 mutt_error ("%s", buffer);
1899 regfree (&SearchRE);
1900 for (i = 0; i < maxLine; i++) {
1902 if (lineInfo[i].search)
1903 FREE (&(lineInfo[i].search));
1904 lineInfo[i].search_cnt = -1;
1911 /* update the search pointers */
1913 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1914 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1915 &QuoteList, &q_level,
1916 &force_redraw, &SearchRE) == 0) {
1918 redraw |= REDRAW_SIDEBAR;
1922 /* searching forward */
1923 for (i = topline; i < lastLine; i++) {
1924 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1925 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1933 /* searching backward */
1934 for (i = topline; i >= 0; i--) {
1935 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1936 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1944 if (lineInfo[topline].search_cnt == 0) {
1946 mutt_error _("Not found.");
1949 SearchFlag = M_SEARCH;
1951 redraw = REDRAW_BODY;
1954 case OP_SEARCH_TOGGLE:
1955 if (SearchCompiled) {
1956 SearchFlag ^= M_SEARCH;
1957 redraw = REDRAW_BODY;
1962 /* don't let the user enter the help-menu from the help screen! */
1965 mutt_help (MENU_PAGER);
1966 redraw = REDRAW_FULL;
1970 mutt_error _("Help is currently being shown.");
1973 case OP_PAGER_HIDE_QUOTED:
1975 hideQuoted ^= M_HIDE;
1976 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1977 topline = upNLines (1, lineInfo, topline, hideQuoted);
1979 redraw = REDRAW_BODY;
1983 case OP_PAGER_SKIP_QUOTED:
1986 int new_topline = topline;
1988 while ((new_topline < lastLine ||
1989 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1990 new_topline, &lastLine,
1991 &maxLine, M_TYPES, &QuoteList,
1992 &q_level, &force_redraw,
1994 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
1995 redraw |= REDRAW_SIDEBAR;
2000 mutt_error _("No more quoted text.");
2005 while ((new_topline < lastLine ||
2006 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2007 new_topline, &lastLine,
2008 &maxLine, M_TYPES, &QuoteList,
2009 &q_level, &force_redraw,
2011 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2013 redraw |= REDRAW_SIDEBAR;
2017 mutt_error _("No more unquoted text after quoted text.");
2021 topline = new_topline;
2025 case OP_PAGER_BOTTOM: /* move to the end of the file */
2026 if (lineInfo[curline].offset < sb.st_size - 1) {
2028 /* make sure the types are defined to the end of file */
2029 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2030 &maxLine, has_types,
2031 &QuoteList, &q_level, &force_redraw,
2034 redraw |= REDRAW_SIDEBAR;
2036 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2039 mutt_error _("Bottom of message is shown.");
2043 clearok (stdscr, TRUE);
2044 redraw = REDRAW_FULL;
2048 km_error_key (MENU_PAGER);
2051 /* --------------------------------------------------------------------
2052 * The following are operations on the current message rather than
2053 * adjusting the view of the message.
2056 case OP_BOUNCE_MESSAGE:
2057 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2059 if (IsMsgAttach (extra))
2060 mutt_attach_bounce (extra->fp, extra->hdr,
2061 extra->idx, extra->idxlen, extra->bdy);
2063 ci_bounce_message (extra->hdr, &redraw);
2067 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2069 if (IsMsgAttach (extra))
2070 mutt_attach_resend (extra->fp, extra->hdr,
2071 extra->idx, extra->idxlen, extra->bdy);
2073 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2074 redraw = REDRAW_FULL;
2077 case OP_CHECK_TRADITIONAL:
2078 CHECK_MODE (IsHeader (extra));
2079 if (!(WithCrypto & APPLICATION_PGP))
2081 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2083 rc = OP_CHECK_TRADITIONAL;
2087 case OP_CREATE_ALIAS:
2088 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2089 if (IsMsgAttach (extra))
2090 mutt_create_alias (extra->bdy->hdr->env, NULL);
2092 mutt_create_alias (extra->hdr->env, NULL);
2093 MAYBE_REDRAW (redraw);
2096 case OP_PURGE_MESSAGE:
2098 CHECK_MODE (IsHeader (extra));
2102 CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2105 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2106 mutt_set_flag (Context, extra->hdr, M_PURGED,
2107 ch != OP_PURGE_MESSAGE ? 0 : 1);
2108 if (option (OPTDELETEUNTAG))
2109 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2110 redraw = REDRAW_STATUS | REDRAW_INDEX;
2111 if (option (OPTRESOLVE)) {
2113 rc = OP_MAIN_NEXT_UNDELETED;
2117 case OP_DELETE_THREAD:
2118 case OP_DELETE_SUBTHREAD:
2119 CHECK_MODE (IsHeader (extra));
2123 CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2126 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2127 ch == OP_DELETE_THREAD ? 0 : 1);
2130 if (option (OPTDELETEUNTAG))
2131 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2132 ch == OP_DELETE_THREAD ? 0 : 1);
2133 if (option (OPTRESOLVE)) {
2134 rc = OP_MAIN_NEXT_UNDELETED;
2138 if (!option (OPTRESOLVE) && PagerIndexLines)
2139 redraw = REDRAW_FULL;
2141 redraw = REDRAW_STATUS | REDRAW_INDEX;
2145 case OP_DISPLAY_ADDRESS:
2146 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2147 if (IsMsgAttach (extra))
2148 mutt_display_address (extra->bdy->hdr->env);
2150 mutt_display_address (extra->hdr->env);
2153 case OP_ENTER_COMMAND:
2154 old_smart_wrap = option (OPTWRAP);
2155 old_markers = option (OPTMARKERS);
2156 old_PagerIndexLines = PagerIndexLines;
2158 CurrentMenu = MENU_PAGER;
2159 mutt_enter_command ();
2161 if (option (OPTNEEDRESORT)) {
2162 unset_option (OPTNEEDRESORT);
2163 CHECK_MODE (IsHeader (extra));
2164 set_option (OPTNEEDRESORT);
2167 if (old_PagerIndexLines != PagerIndexLines) {
2169 mutt_menuDestroy (&index);
2173 if (option (OPTWRAP) != old_smart_wrap ||
2174 option (OPTMARKERS) != old_markers) {
2175 if (flags & M_PAGER_RETWINCH) {
2177 rc = OP_REFORMAT_WINCH;
2181 /* count the real lines above */
2183 for (i = 0; i <= topline; i++) {
2184 if (!lineInfo[i].continuation)
2188 /* we need to restart the whole thing */
2189 for (i = 0; i < maxLine; i++) {
2190 lineInfo[i].offset = 0;
2191 lineInfo[i].type = -1;
2192 lineInfo[i].continuation = 0;
2193 lineInfo[i].chunks = 0;
2194 lineInfo[i].search_cnt = -1;
2195 lineInfo[i].quote = NULL;
2197 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2198 if (SearchCompiled && lineInfo[i].search)
2199 FREE (&(lineInfo[i].search));
2202 if (SearchCompiled) {
2203 regfree (&SearchRE);
2208 /* try to keep the old position */
2211 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2212 &lastLine, &maxLine,
2213 (has_types ? M_TYPES : 0),
2214 &QuoteList, &q_level, &force_redraw,
2216 redraw |= REDRAW_SIDEBAR;
2217 if (!lineInfo[topline].continuation)
2226 if (option (OPTFORCEREDRAWPAGER))
2227 redraw = REDRAW_FULL;
2228 unset_option (OPTFORCEREDRAWINDEX);
2229 unset_option (OPTFORCEREDRAWPAGER);
2232 case OP_FLAG_MESSAGE:
2233 CHECK_MODE (IsHeader (extra));
2237 if (Context->magic == M_POP) {
2239 mutt_error _("Can't change 'important' flag on POP server.");
2246 CHECK_IMAP_ACL (IMAP_ACL_WRITE);
2250 if (Context->magic == M_NNTP) {
2252 mutt_error _("Can't change 'important' flag on NNTP server.");
2258 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2259 redraw = REDRAW_STATUS | REDRAW_INDEX;
2260 if (option (OPTRESOLVE)) {
2262 rc = OP_MAIN_NEXT_UNDELETED;
2267 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2268 if (IsAttach (extra))
2269 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2271 mutt_pipe_message (extra->hdr);
2272 MAYBE_REDRAW (redraw);
2276 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2277 if (IsAttach (extra))
2278 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2280 mutt_print_message (extra->hdr);
2284 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2286 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2287 redraw = REDRAW_FULL;
2292 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2294 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2295 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2296 query_quadoption (OPT_TOMODERATED,
2298 ("Posting to this group not allowed, may be moderated. Continue?"))
2301 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2302 redraw = REDRAW_FULL;
2305 case OP_FORWARD_TO_GROUP:
2306 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2308 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2309 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2310 query_quadoption (OPT_TOMODERATED,
2312 ("Posting to this group not allowed, may be moderated. Continue?"))
2315 if (IsMsgAttach (extra))
2316 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2317 extra->idxlen, extra->bdy, SENDNEWS);
2319 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2321 redraw = REDRAW_FULL;
2325 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2328 if (IsMsgAttach (extra))
2329 followup_to = extra->bdy->hdr->env->followup_to;
2331 followup_to = extra->hdr->env->followup_to;
2333 if (!followup_to || mutt_strcasecmp (followup_to, "poster") ||
2334 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2335 _("Reply by mail as poster prefers?")) != M_YES) {
2336 if (extra->ctx && extra->ctx->magic == M_NNTP
2337 && !((NNTP_DATA *) extra->ctx->data)->allowed
2338 && query_quadoption (OPT_TOMODERATED,
2340 ("Posting to this group not allowed, may be moderated. Continue?"))
2343 if (IsMsgAttach (extra))
2344 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2345 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2347 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2348 extra->ctx, extra->hdr);
2349 redraw = REDRAW_FULL;
2355 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2357 if (IsMsgAttach (extra))
2358 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2359 extra->idxlen, extra->bdy, SENDREPLY);
2361 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2362 redraw = REDRAW_FULL;
2365 case OP_RECALL_MESSAGE:
2366 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2368 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2369 redraw = REDRAW_FULL;
2372 case OP_GROUP_REPLY:
2373 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2375 if (IsMsgAttach (extra))
2376 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2377 extra->idxlen, extra->bdy,
2378 SENDREPLY | SENDGROUPREPLY);
2380 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2382 redraw = REDRAW_FULL;
2386 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2388 if (IsMsgAttach (extra))
2389 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2390 extra->idxlen, extra->bdy,
2391 SENDREPLY | SENDLISTREPLY);
2393 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2395 redraw = REDRAW_FULL;
2398 case OP_FORWARD_MESSAGE:
2399 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2401 if (IsMsgAttach (extra))
2402 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2403 extra->idxlen, extra->bdy, 0);
2405 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2406 redraw = REDRAW_FULL;
2409 case OP_DECRYPT_SAVE:
2416 if (IsAttach (extra)) {
2417 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2422 case OP_COPY_MESSAGE:
2423 case OP_DECODE_SAVE:
2424 case OP_DECODE_COPY:
2425 case OP_DECRYPT_COPY:
2426 if (!WithCrypto && ch == OP_DECRYPT_COPY) {
2430 CHECK_MODE (IsHeader (extra));
2431 if (mutt_save_message (extra->hdr,
2432 (ch == OP_DECRYPT_SAVE) ||
2433 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2434 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2435 (ch == OP_DECRYPT_SAVE)
2436 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2437 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2438 || ch == OP_DECRYPT_SAVE)) {
2439 if (option (OPTRESOLVE)) {
2441 rc = OP_MAIN_NEXT_UNDELETED;
2444 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2446 MAYBE_REDRAW (redraw);
2449 case OP_SHELL_ESCAPE:
2450 mutt_shell_escape ();
2451 MAYBE_REDRAW (redraw);
2455 CHECK_MODE (IsHeader (extra));
2456 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2458 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2459 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2460 ? NULL : Context->last_tag);
2462 redraw = REDRAW_STATUS | REDRAW_INDEX;
2463 if (option (OPTRESOLVE)) {
2470 CHECK_MODE (IsHeader (extra));
2474 CHECK_IMAP_ACL (IMAP_ACL_SEEN);
2477 if (extra->hdr->read || extra->hdr->old)
2478 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2480 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2482 Context->msgnotreadyet = -1;
2483 redraw = REDRAW_STATUS | REDRAW_INDEX;
2484 if (option (OPTRESOLVE)) {
2486 rc = OP_MAIN_NEXT_UNDELETED;
2491 CHECK_MODE (IsHeader (extra));
2495 CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2498 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2499 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2500 redraw = REDRAW_STATUS | REDRAW_INDEX;
2501 if (option (OPTRESOLVE)) {
2507 case OP_UNDELETE_THREAD:
2508 case OP_UNDELETE_SUBTHREAD:
2509 CHECK_MODE (IsHeader (extra));
2513 CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2516 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2517 ch == OP_UNDELETE_THREAD ? 0 : 1)
2518 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2519 ch == OP_UNDELETE_THREAD ? 0 : 1);
2522 if (option (OPTRESOLVE)) {
2523 rc = (ch == OP_DELETE_THREAD) ?
2524 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2528 if (!option (OPTRESOLVE) && PagerIndexLines)
2529 redraw = REDRAW_FULL;
2531 redraw = REDRAW_STATUS | REDRAW_INDEX;
2541 redraw |= REDRAW_SIDEBAR;
2544 case OP_VIEW_ATTACHMENTS:
2545 if (flags & M_PAGER_ATTACHMENT) {
2547 rc = OP_ATTACH_COLLAPSE;
2550 CHECK_MODE (IsHeader (extra));
2551 mutt_view_attachments (extra->hdr);
2552 if (extra->hdr->attach_del)
2553 Context->changed = 1;
2554 redraw = REDRAW_FULL;
2559 if (!(WithCrypto & APPLICATION_PGP)) {
2563 CHECK_MODE (IsHeader (extra));
2565 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2566 redraw = REDRAW_FULL;
2570 case OP_FORGET_PASSPHRASE:
2571 crypt_forget_passphrase ();
2574 case OP_EXTRACT_KEYS:
2579 CHECK_MODE (IsHeader (extra));
2580 crypt_extract_keys_from_messages (extra->hdr);
2581 redraw = REDRAW_FULL;
2584 case OP_SIDEBAR_SCROLL_UP:
2585 case OP_SIDEBAR_SCROLL_DOWN:
2586 case OP_SIDEBAR_NEXT:
2587 case OP_SIDEBAR_NEXT_NEW:
2588 case OP_SIDEBAR_PREV:
2589 case OP_SIDEBAR_PREV_NEW:
2590 scroll_sidebar (ch, MENU_PAGER);
2599 if (IsHeader (extra))
2600 Context->msgnotreadyet = -1;
2602 cleanup_quote (&QuoteList);
2604 for (i = 0; i < maxLine; i++) {
2605 FREE (&(lineInfo[i].syntax));
2606 if (SearchCompiled && lineInfo[i].search)
2607 FREE (&(lineInfo[i].search));
2609 if (SearchCompiled) {
2610 regfree (&SearchRE);
2615 mutt_menuDestroy (&index);
2616 return (rc != -1 ? rc : 0);