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.
20 #include "mutt_curses.h"
21 #include "mutt_regex.h"
23 #include "mutt_menu.h"
33 #include "imap_private.h"
36 #include "mutt_crypt.h"
45 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
47 #define IsAttach(x) (x && (x)->bdy)
48 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
49 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
50 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
51 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
53 static const char *Not_available_in_this_menu = N_("Not available in this menu.");
54 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
55 static const char *Function_not_permitted_in_attach_message_mode = N_("Function not permitted in attach-message mode.");
57 #define CHECK_MODE(x) if (!(x)) \
60 mutt_error _(Not_available_in_this_menu); \
64 #define CHECK_READONLY if (Context->readonly) \
67 mutt_error _(Mailbox_is_read_only); \
71 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
74 mutt_error _(Function_not_permitted_in_attach_message_mode); \
79 /* the error message returned here could be better. */
80 #define CHECK_IMAP_ACL(aclbit) if (Context->magic == M_IMAP) \
81 if (mutt_bit_isset (((IMAP_DATA *)Context->data)->capabilities, ACL) \
82 && !mutt_bit_isset(((IMAP_DATA *)Context->data)->rights,aclbit)){ \
84 mutt_error ("Operation not permitted by the IMAP ACL for this mailbox"); \
95 struct q_class_t *next, *prev;
96 struct q_class_t *down, *up;
113 struct syntax_t *syntax;
114 struct syntax_t *search;
115 struct q_class_t *quote;
118 #define ANSI_OFF (1<<0)
119 #define ANSI_BLINK (1<<1)
120 #define ANSI_BOLD (1<<2)
121 #define ANSI_UNDERLINE (1<<3)
122 #define ANSI_REVERSE (1<<4)
123 #define ANSI_COLOR (1<<5)
125 typedef struct _ansi_attr {
132 static short InHelp = 0;
134 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
135 static struct resize {
142 #define NumSigLines 4
144 static int check_sig (const char *s, struct line_t *info, int n)
148 while (n > 0 && count <= NumSigLines)
150 if (info[n].type != MT_COLOR_SIGNATURE)
159 if (count > NumSigLines)
161 /* check for a blank line */
176 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags, int special,
179 int def_color; /* color without syntax hilight */
180 int color; /* final color */
181 static int last_color; /* last color set */
182 int search = 0, i, m;
185 last_color = -1; /* force attrset() */
187 if (lineInfo[n].continuation)
189 if (!cnt && option (OPTMARKERS))
191 SETCOLOR (MT_COLOR_MARKERS);
193 last_color = ColorDefs[MT_COLOR_MARKERS];
195 m = (lineInfo[n].syntax)[0].first;
196 cnt += (lineInfo[n].syntax)[0].last;
200 if (!(flags & M_SHOWCOLOR))
201 def_color = ColorDefs[MT_COLOR_NORMAL];
202 else if (lineInfo[m].type == MT_COLOR_HEADER)
203 def_color = (lineInfo[m].syntax)[0].color;
205 def_color = ColorDefs[lineInfo[m].type];
207 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED)
209 struct q_class_t *class = lineInfo[m].quote;
213 def_color = class->color;
215 while (class && class->length > cnt)
217 def_color = class->color;
224 if (flags & M_SHOWCOLOR)
226 for (i = 0; i < lineInfo[m].chunks; i++)
228 /* we assume the chunks are sorted */
229 if (cnt > (lineInfo[m].syntax)[i].last)
231 if (cnt < (lineInfo[m].syntax)[i].first)
233 if (cnt != (lineInfo[m].syntax)[i].last)
235 color = (lineInfo[m].syntax)[i].color;
238 /* don't break here, as cnt might be
239 * in the next chunk as well */
243 if (flags & M_SEARCH)
245 for (i = 0; i < lineInfo[m].search_cnt; i++)
247 if (cnt > (lineInfo[m].search)[i].last)
249 if (cnt < (lineInfo[m].search)[i].first)
251 if (cnt != (lineInfo[m].search)[i].last)
253 color = ColorDefs[MT_COLOR_SEARCH];
260 /* handle "special" bold & underlined characters */
261 if (special || a->attr)
264 if ((a->attr & ANSI_COLOR))
267 a->pair = mutt_alloc_color (a->fg, a->bg);
269 if (a->attr & ANSI_BOLD)
274 if ((special & A_BOLD) || (a->attr & ANSI_BOLD))
276 if (ColorDefs[MT_COLOR_BOLD] && !search)
277 color = ColorDefs[MT_COLOR_BOLD];
281 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE))
283 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
284 color = ColorDefs[MT_COLOR_UNDERLINE];
286 color ^= A_UNDERLINE;
288 else if (a->attr & ANSI_REVERSE)
292 else if (a->attr & ANSI_BLINK)
296 else if (a->attr & ANSI_OFF)
302 if (color != last_color)
310 append_line (struct line_t *lineInfo, int n, int cnt)
314 lineInfo[n+1].type = lineInfo[n].type;
315 (lineInfo[n+1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
316 lineInfo[n+1].continuation = 1;
318 /* find the real start of the line */
319 for (m = n; m >= 0; m--)
320 if (lineInfo[m].continuation == 0) break;
322 (lineInfo[n+1].syntax)[0].first = m;
323 (lineInfo[n+1].syntax)[0].last = (lineInfo[n].continuation) ?
324 cnt + (lineInfo[n].syntax)[0].last : cnt;
328 new_class_color (struct q_class_t *class, int *q_level)
330 class->index = (*q_level)++;
331 class->color = ColorQuote[class->index % ColorQuoteUsed];
335 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
336 int index, int *q_level)
338 struct q_class_t * q_list;
341 new_class->index = -1;
345 if (q_list->index >= index)
348 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
351 q_list = q_list->down;
352 else if (q_list->next)
353 q_list = q_list->next;
356 while (!q_list->next)
363 q_list = q_list->next;
367 new_class->index = index;
368 new_class->color = ColorQuote[index % ColorQuoteUsed];
373 cleanup_quote (struct q_class_t **QuoteList)
375 struct q_class_t *ptr;
379 if ((*QuoteList)->down)
380 cleanup_quote (&((*QuoteList)->down));
381 ptr = (*QuoteList)->next;
382 if ((*QuoteList)->prefix)
383 FREE (&(*QuoteList)->prefix);
391 static struct q_class_t *
392 classify_quote (struct q_class_t **QuoteList, const char *qptr,
393 int length, int *force_redraw, int *q_level)
395 struct q_class_t *q_list = *QuoteList;
396 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
398 int offset, tail_lng;
401 if (ColorQuoteUsed <= 1)
403 /* not much point in classifying quotes... */
405 if (*QuoteList == NULL)
407 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
408 class->color = ColorQuote[0];
414 /* Did I mention how much I like emulating Lisp in C? */
416 /* classify quoting prefix */
419 if (length <= q_list->length)
421 /* case 1: check the top level nodes */
423 if (mutt_strncmp (qptr, q_list->prefix, length) == 0)
425 if (length == q_list->length)
426 return q_list; /* same prefix: return the current class */
428 /* found shorter prefix */
431 /* add a node above q_list */
432 tmp = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
433 tmp->prefix = (char *) safe_calloc (1, length + 1);
434 strncpy (tmp->prefix, qptr, length);
435 tmp->length = length;
437 /* replace q_list by tmp in the top level list */
440 tmp->next = q_list->next;
441 q_list->next->prev = tmp;
445 tmp->prev = q_list->prev;
446 q_list->prev->next = tmp;
449 /* make q_list a child of tmp */
453 /* q_list has no siblings for now */
457 /* update the root if necessary */
458 if (q_list == *QuoteList)
461 index = q_list->index;
463 /* tmp should be the return class too */
466 /* next class to test; if tmp is a shorter prefix for another
467 * node, that node can only be in the top level list, so don't
468 * go down after this point
474 /* found another branch for which tmp is a shorter prefix */
476 /* save the next sibling for later */
479 /* unlink q_list from the top level list */
481 q_list->next->prev = q_list->prev;
483 q_list->prev->next = q_list->next;
485 /* at this point, we have a tmp->down; link q_list to it */
487 /* sibling order is important here, q_list should be linked last */
495 index = q_list->index;
497 /* next class to test; as above, we shouldn't go down */
501 /* we found a shorter prefix, so certain quotes have changed classes */
507 /* shorter, but not a substring of the current class: try next */
508 q_list = q_list->next;
514 /* case 2: try subclassing the current top level node */
516 /* tmp != NULL means we already found a shorter prefix at case 1 */
517 if (tmp == NULL && mutt_strncmp (qptr, q_list->prefix, q_list->length) == 0)
519 /* ok, it's a subclass somewhere on this branch */
522 offset = q_list->length;
524 q_list = q_list->down;
525 tail_lng = length - offset;
526 tail_qptr = (char *) qptr + offset;
530 if (length <= q_list->length)
532 if (mutt_strncmp (tail_qptr, (q_list->prefix) + offset, tail_lng) == 0)
534 /* same prefix: return the current class */
535 if (length == q_list->length)
538 /* found shorter common prefix */
541 /* add a node above q_list */
542 tmp = (struct q_class_t *) safe_calloc (1,
543 sizeof (struct q_class_t));
544 tmp->prefix = (char *) safe_calloc (1, length + 1);
545 strncpy (tmp->prefix, qptr, length);
546 tmp->length = length;
548 /* replace q_list by tmp */
551 tmp->next = q_list->next;
552 q_list->next->prev = tmp;
556 tmp->prev = q_list->prev;
557 q_list->prev->next = tmp;
560 /* make q_list a child of tmp */
562 tmp->up = q_list->up;
564 if (tmp->up->down == q_list)
567 /* q_list has no siblings */
571 index = q_list->index;
573 /* tmp should be the return class too */
576 /* next class to test */
581 /* found another branch for which tmp is a shorter prefix */
583 /* save the next sibling for later */
586 /* unlink q_list from the top level list */
588 q_list->next->prev = q_list->prev;
590 q_list->prev->next = q_list->next;
592 /* at this point, we have a tmp->down; link q_list to it */
601 index = q_list->index;
603 /* next class to test */
607 /* we found a shorter prefix, so we need a redraw */
613 q_list = q_list->next;
619 /* longer than the current prefix: try subclassing it */
620 if (tmp == NULL && mutt_strncmp (tail_qptr, (q_list->prefix) + offset,
621 q_list->length - offset) == 0)
623 /* still a subclass: go down one level */
625 offset = q_list->length;
627 q_list = q_list->down;
628 tail_lng = length - offset;
629 tail_qptr = (char *) qptr + offset;
635 /* nope, try the next prefix */
636 q_list = q_list->next;
642 /* still not found so far: add it as a sibling to the current node */
645 tmp = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
646 tmp->prefix = (char *) safe_calloc (1, length + 1);
647 strncpy (tmp->prefix, qptr, length);
648 tmp->length = length;
652 tmp->next = ptr->down;
653 ptr->down->prev = tmp;
658 new_class_color (tmp, q_level);
665 shift_class_colors (*QuoteList, tmp, index, q_level);
672 /* nope, try the next prefix */
673 q_list = q_list->next;
681 /* not found so far: add it as a top level class */
682 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
683 class->prefix = (char *) safe_calloc (1, length + 1);
684 strncpy (class->prefix, qptr, length);
685 class->length = length;
686 new_class_color (class, q_level);
690 class->next = *QuoteList;
691 (*QuoteList)->prev = class;
697 shift_class_colors (*QuoteList, tmp, index, q_level);
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))
716 lineInfo[n].type = MT_COLOR_NORMAL;
717 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t'))
719 lineInfo[n].type = lineInfo[n-1].type; /* wrapped line */
720 (lineInfo[n].syntax)[0].color = (lineInfo[n-1].syntax)[0].color;
724 lineInfo[n].type = MT_COLOR_HDEFAULT;
725 color_line = ColorHdrList;
728 if (REGEXEC (color_line->rx, buf) == 0)
730 lineInfo[n].type = MT_COLOR_HEADER;
731 lineInfo[n].syntax[0].color = color_line->pair;
734 color_line = color_line->next;
738 else if (mutt_strncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
739 lineInfo[n].type = MT_COLOR_NORMAL;
741 else if (mutt_strncmp ("[-- ", buf, 4) == 0)
742 lineInfo[n].type = MT_COLOR_ATTACHMENT;
744 else if (check_attachment_marker ((char *) raw) == 0)
745 lineInfo[n].type = MT_COLOR_ATTACHMENT;
747 else if (mutt_strcmp ("-- \n", buf) == 0 || mutt_strcmp ("-- \r\n", buf) == 0)
751 lineInfo[n].type = MT_COLOR_SIGNATURE;
752 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
753 (lineInfo[i].type == MT_COLOR_NORMAL ||
754 lineInfo[i].type == MT_COLOR_QUOTED ||
755 lineInfo[i].type == MT_COLOR_HEADER))
758 if (lineInfo[i].chunks)
760 lineInfo[i].chunks = 0;
761 safe_realloc (&(lineInfo[n].syntax),
762 sizeof (struct syntax_t));
764 lineInfo[i++].type = MT_COLOR_SIGNATURE;
767 else if (check_sig (buf, lineInfo, n - 1) == 0)
768 lineInfo[n].type = MT_COLOR_SIGNATURE;
769 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0)
771 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0)
773 if (smatch[0].rm_so > 0)
777 /* hack to avoid making an extra copy of buf */
778 c = buf[smatch[0].rm_so];
779 buf[smatch[0].rm_so] = 0;
781 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0)
783 if (q_classify && lineInfo[n].quote == NULL)
784 lineInfo[n].quote = classify_quote (QuoteList,
785 buf + pmatch[0].rm_so,
786 pmatch[0].rm_eo - pmatch[0].rm_so,
787 force_redraw, q_level);
788 lineInfo[n].type = MT_COLOR_QUOTED;
791 lineInfo[n].type = MT_COLOR_NORMAL;
793 buf[smatch[0].rm_so] = c;
796 lineInfo[n].type = MT_COLOR_NORMAL;
800 if (q_classify && lineInfo[n].quote == NULL)
801 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
802 pmatch[0].rm_eo - pmatch[0].rm_so,
803 force_redraw, q_level);
804 lineInfo[n].type = MT_COLOR_QUOTED;
808 lineInfo[n].type = MT_COLOR_NORMAL;
811 if (lineInfo[n].type == MT_COLOR_NORMAL ||
812 lineInfo[n].type == MT_COLOR_QUOTED)
817 lineInfo[n].chunks = 0;
825 color_line = ColorBodyList;
828 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
829 (offset ? REG_NOTBOL : 0)) == 0)
831 if (pmatch[0].rm_eo != pmatch[0].rm_so)
835 if (++(lineInfo[n].chunks) > 1)
836 safe_realloc (&(lineInfo[n].syntax),
837 (lineInfo[n].chunks) * sizeof (struct syntax_t));
839 i = lineInfo[n].chunks - 1;
840 pmatch[0].rm_so += offset;
841 pmatch[0].rm_eo += offset;
843 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
844 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
845 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last))
847 (lineInfo[n].syntax)[i].color = color_line->pair;
848 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
849 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
855 null_rx = 1; /* empty regexp; don't add it, but keep looking */
857 color_line = color_line->next;
861 offset++; /* avoid degenerate cases */
863 offset = (lineInfo[n].syntax)[i].last;
864 } while (found || null_rx);
868 static int is_ansi (unsigned char *buf)
870 while (*buf && (isdigit(*buf) || *buf == ';'))
872 return (*buf == 'm');
875 static int check_attachment_marker (char *p)
877 char *q = AttachmentMarker;
879 for (;*p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++)
881 return (int) (*p - *q);
884 static int grok_ansi(unsigned char *buf, int pos, ansi_attr *a)
888 while (isdigit(buf[x]) || buf[x] == ';')
891 /* Character Attributes */
892 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm')
898 mutt_free_color (a->fg, a->bg);
905 if (buf[pos] == '1' && (pos+1 == x || buf[pos+1] == ';'))
907 a->attr |= ANSI_BOLD;
910 else if (buf[pos] == '4' && (pos+1 == x || buf[pos+1] == ';'))
912 a->attr |= ANSI_UNDERLINE;
915 else if (buf[pos] == '5' && (pos+1 == x || buf[pos+1] == ';'))
917 a->attr |= ANSI_BLINK;
920 else if (buf[pos] == '7' && (pos+1 == x || buf[pos+1] == ';'))
922 a->attr |= ANSI_REVERSE;
925 else if (buf[pos] == '0' && (pos+1 == x || buf[pos+1] == ';'))
929 mutt_free_color(a->fg,a->bg);
935 else if (buf[pos] == '3' && isdigit(buf[pos+1]))
939 mutt_free_color(a->fg,a->bg);
942 a->attr |= ANSI_COLOR;
943 a->fg = buf[pos+1] - '0';
946 else if (buf[pos] == '4' && isdigit(buf[pos+1]))
950 mutt_free_color(a->fg,a->bg);
953 a->attr |= ANSI_COLOR;
954 a->bg = buf[pos+1] - '0';
959 while (pos < x && buf[pos] != ';') pos++;
969 fill_buffer (FILE *f, long *last_pos, long offset, unsigned char *buf,
970 unsigned char *fmt, size_t blen, int *buf_ready)
978 if (offset != *last_pos)
979 fseek (f, offset, 0);
980 if (fgets ((char *) buf, blen - 1, f) == NULL)
985 *last_pos = ftell (f);
986 b_read = (int) (*last_pos - offset);
989 /* copy "buf" to "fmt", but without bold and underline controls */
993 if (*p == '\010' && (p > buf))
995 if (*(p+1) == '_') /* underline */
997 else if (*(p+1)) /* bold or overstrike */
1005 else if (*p == '\033' && *(p+1) == '[' && is_ansi (p + 2))
1007 while (*p++ != 'm') /* skip ANSI sequence */
1010 else if (*p == '\033' && *(p+1) == ']' && check_attachment_marker ((char *) p) == 0)
1012 dprint (2, (debugfile, "fill_buffer: Seen attachment marker.\n"));
1013 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
1030 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
1031 int flags, ansi_attr *pa, int cnt,
1032 int *pspace, int *pvch, int *pcol, int *pspecial)
1034 int space = -1; /* index of the last space or TAB */
1035 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
1036 int ch, vch, k, last_special = -1, special = 0, t;
1040 int wrap_cols = COLS - WrapMargin;
1045 /* FIXME: this should come from lineInfo */
1046 memset(&mbstate, 0, sizeof(mbstate));
1048 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k)
1050 /* Handle ANSI sequences */
1051 while (cnt-ch >= 2 && buf[ch] == '\033' && buf[ch+1] == '[' &&
1053 ch = grok_ansi (buf, ch+2, pa) + 1;
1055 while (cnt-ch >= 2 && buf[ch] == '\033' && buf[ch+1] == ']' &&
1056 check_attachment_marker ((char *) buf+ch) == 0)
1058 while (buf[ch++] != '\a')
1063 /* is anything left to do? */
1067 k = mbrtowc (&wc, (char *)buf+ch, cnt-ch, &mbstate);
1068 if (k == -2 || k == -1)
1070 dprint (1, (debugfile, "%s:%d: mbrtowc returned %d; errno = %d.\n",
1071 __FILE__, __LINE__, k, errno));
1072 if (col + 4 > wrap_cols)
1076 printw ("\\%03o", buf[ch]);
1083 /* Handle backspace */
1091 while ((wc1 = 0, mbstate1 = mbstate,
1092 k1 = k + mbrtowc (&wc1, (char *)buf+ch+k, cnt-ch-k, &mbstate1),
1093 k1 - k > 0 && wc1 == '\b') &&
1095 k2 = mbrtowc (&wc1, (char *)buf+ch+k1, cnt-ch-k1, &mbstate1),
1096 k2 > 0 && IsWPrint (wc1)))
1100 special |= (wc == '_' && special & A_UNDERLINE)
1101 ? A_UNDERLINE : A_BOLD;
1103 else if (wc == '_' || wc1 == '_')
1105 special |= A_UNDERLINE;
1106 wc = (wc1 == '_') ? wc : wc1;
1110 /* special = 0; / * overstrike: nothing to do! */
1120 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1121 special || last_special || pa->attr))
1123 resolve_color (*lineInfo, n, vch, flags, special, pa);
1124 last_special = special;
1132 if (col + t > wrap_cols)
1138 else if (wc == '\n')
1140 else if (wc == '\t')
1147 for (; col < t; col++)
1152 else if (wc < 0x20 || wc == 0x7f)
1154 if (col + 2 > wrap_cols)
1158 printw ("^%c", ('@' + wc) & 0x7f);
1160 else if (wc < 0x100)
1162 if (col + 4 > wrap_cols)
1166 printw ("\\%03o", wc);
1170 if (col + 1 > wrap_cols)
1174 addch (replacement_char ());
1180 *pspecial = special;
1186 * flags M_SHOWFLAT, show characters (used for displaying help)
1187 * M_SHOWCOLOR, show characters in color
1188 * otherwise don't show characters
1189 * M_HIDE, don't show quoted text
1190 * M_SEARCH, resolve search patterns
1191 * M_TYPES, compute line's type
1192 * M_PAGER_NSKIP, keeps leading whitespace
1193 * M_PAGER_MARKER, eventually show markers
1196 * -1 EOF was reached
1197 * 0 normal exit, line was not displayed
1198 * >0 normal exit, line was displayed
1202 display_line (FILE *f, long *last_pos, struct line_t **lineInfo, int n,
1203 int *last, int *max, int flags, struct q_class_t **QuoteList,
1204 int *q_level, int *force_redraw, regex_t *SearchRE)
1206 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1207 unsigned char *buf_ptr = buf;
1208 int ch, vch, col, cnt, b_read;
1209 int buf_ready = 0, change_last = 0;
1214 ansi_attr a = {0,0,0,-1};
1215 regmatch_t pmatch[1];
1225 safe_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1226 for (ch = *last; ch < *max ; ch++)
1228 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1229 (*lineInfo)[ch].type = -1;
1230 (*lineInfo)[ch].search_cnt = -1;
1231 (*lineInfo)[ch].syntax = safe_malloc (sizeof (struct syntax_t));
1232 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last = -1;
1236 /* only do color hiliting if we are viewing a message */
1237 if (flags & (M_SHOWCOLOR | M_TYPES))
1239 if ((*lineInfo)[n].type == -1)
1241 /* determine the line class */
1242 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1249 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1250 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1252 /* avoid race condition for continuation lines when scrolling up */
1253 for (m = n + 1; m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation; m++)
1254 (*lineInfo)[m].type = (*lineInfo)[n].type;
1257 /* this also prevents searching through the hidden lines */
1258 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1259 flags = 0; /* M_NOSHOW */
1262 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1263 * don't want to compute it every time M_TYPES is set, since this
1264 * would slow down the "bottom" function unacceptably. A compromise
1265 * solution is hence to call regexec() again, just to find out the
1266 * length of the quote prefix.
1268 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1269 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1271 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1277 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1278 (*lineInfo)[n].quote = classify_quote (QuoteList,
1279 (char *) fmt + pmatch[0].rm_so,
1280 pmatch[0].rm_eo - pmatch[0].rm_so,
1281 force_redraw, q_level);
1284 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation && (*lineInfo)[n].search_cnt == -1)
1286 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1294 (*lineInfo)[n].search_cnt = 0;
1295 while (regexec (SearchRE, (char *) fmt + offset, 1, pmatch, (offset ? REG_NOTBOL : 0)) == 0)
1297 if (++((*lineInfo)[n].search_cnt) > 1)
1298 safe_realloc (&((*lineInfo)[n].search),
1299 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1301 (*lineInfo)[n].search = safe_malloc (sizeof (struct syntax_t));
1302 pmatch[0].rm_so += offset;
1303 pmatch[0].rm_eo += offset;
1304 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first = pmatch[0].rm_so;
1305 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last = pmatch[0].rm_eo;
1307 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1308 offset++; /* avoid degenerate cases */
1310 offset = pmatch[0].rm_eo;
1316 if (!(flags & M_SHOW) && (*lineInfo)[n+1].offset > 0)
1318 /* we've already scanned this line, so just exit */
1321 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n+1].offset > 0)
1323 /* no need to try to display this line... */
1324 return (1); /* fake display */
1327 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1328 sizeof (buf), &buf_ready)) < 0)
1335 /* now chose a good place to break the line */
1336 cnt = format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col, &special);
1337 buf_ptr = buf + cnt;
1339 /* move the break point only if smart_wrap is set */
1340 if (option (OPTWRAP))
1344 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n' && buf[cnt] != '\r')
1347 /* skip trailing blanks */
1348 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1353 buf_ptr = buf + cnt; /* a very long word... */
1355 if (!(flags & M_PAGER_NSKIP))
1356 /* skip leading blanks on the next line too */
1357 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1361 if (*buf_ptr == '\r')
1363 if (*buf_ptr == '\n')
1366 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n+1].continuation)
1367 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1368 (*lineInfo)[n+1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1370 /* if we don't need to display the line we are done */
1371 if (!(flags & M_SHOW))
1374 /* display the line */
1375 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1377 /* avoid a bug in ncurses... */
1378 #ifndef USE_SLANG_CURSES
1381 SETCOLOR (MT_COLOR_NORMAL);
1386 /* end the last color pattern (needed by S-Lang) */
1387 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1388 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1391 * Fill the blank space at the end of the line with the prevailing color.
1392 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1393 * to make sure to reset the color *after* that
1395 if (flags & M_SHOWCOLOR)
1397 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1398 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1399 def_color = ((*lineInfo)[m].syntax)[0].color;
1401 def_color = ColorDefs[ (*lineInfo)[m].type ];
1403 attrset (def_color);
1405 bkgdset (def_color | ' ');
1409 /* ncurses always wraps lines when you get to the right side of the
1410 * screen, but S-Lang seems to only wrap if the next character is *not*
1413 #ifndef USE_SLANG_CURSES
1419 * reset the color back to normal. This *must* come after the
1420 * addch('\n'), otherwise the color for this line will not be
1421 * filled to the right margin.
1423 if (flags & M_SHOWCOLOR)
1425 SETCOLOR(MT_COLOR_NORMAL);
1426 BKGDSET(MT_COLOR_NORMAL);
1429 /* build a return code */
1430 if (!(flags & M_SHOW))
1437 upNLines (int nlines, struct line_t *info, int cur, int hiding)
1439 while (cur > 0 && nlines > 0)
1442 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1449 static struct mapping_t PagerHelp[] = {
1450 { N_("Exit"), OP_EXIT },
1451 { N_("PrevPg"), OP_PREV_PAGE },
1452 { N_("NextPg"), OP_NEXT_PAGE },
1455 static struct mapping_t PagerHelpExtra[] = {
1456 { N_("View Attachm."), OP_VIEW_ATTACHMENTS },
1457 { N_("Del"), OP_DELETE },
1458 { N_("Reply"), OP_REPLY },
1459 { N_("Next"), OP_MAIN_NEXT_UNDELETED },
1464 static struct mapping_t PagerNewsHelpExtra[] = {
1465 { N_("Post"), OP_POST },
1466 { N_("Followup"), OP_FOLLOWUP },
1467 { N_("Del"), OP_DELETE },
1468 { N_("Next"), OP_MAIN_NEXT_UNDELETED },
1475 /* This pager is actually not so simple as it once was. It now operates in
1476 two modes: one for viewing messages and the other for viewing help. These
1477 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1478 is there so that we can do operations on the current message without the
1479 need to pop back out to the main-menu. */
1481 mutt_pager (const char *banner, const char *fname, int flags, pager_t *extra)
1483 static char searchbuf[STRING];
1484 char buffer[LONG_STRING];
1485 char helpstr[SHORT_STRING*2];
1486 char tmphelp[SHORT_STRING*2];
1487 int maxLine, lastLine = 0;
1488 struct line_t *lineInfo;
1489 struct q_class_t *QuoteList = NULL;
1490 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1491 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1493 int redraw = REDRAW_FULL;
1495 long last_pos = 0, last_offset = 0;
1496 int old_smart_wrap, old_markers;
1499 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1500 int has_types = (IsHeader(extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1502 int bodyoffset = 1; /* offset of first line of real text */
1503 int statusoffset = 0; /* offset for the status bar */
1504 int helpoffset = LINES - 2; /* offset for the help bar. */
1505 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1507 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1508 int indexoffset = 0; /* offset for the PI */
1509 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1510 int indicator = indexlen / 3; /* the indicator line of the PI */
1511 int old_PagerIndexLines; /* some people want to resize it
1512 * while inside the pager... */
1518 if (!(flags & M_SHOWCOLOR))
1519 flags |= M_SHOWFLAT;
1521 if ((fp = fopen (fname, "r")) == NULL)
1523 mutt_perror (fname);
1527 if (stat (fname, &sb) != 0)
1529 mutt_perror (fname);
1535 /* Initialize variables */
1537 if (IsHeader (extra) && !extra->hdr->read)
1539 Context->msgnotreadyet = extra->hdr->msgno;
1540 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1543 lineInfo = safe_malloc (sizeof (struct line_t) * (maxLine = LINES));
1544 for (i = 0 ; i < maxLine ; i++)
1546 memset (&lineInfo[i], 0, sizeof (struct line_t));
1547 lineInfo[i].type = -1;
1548 lineInfo[i].search_cnt = -1;
1549 lineInfo[i].syntax = safe_malloc (sizeof (struct syntax_t));
1550 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1553 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1554 if (IsHeader (extra))
1556 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1557 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1559 (Context && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1562 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1566 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1567 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1568 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1579 if (redraw & REDRAW_FULL)
1581 SETCOLOR (MT_COLOR_NORMAL);
1582 /* clear() doesn't optimize screen redraws */
1586 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1587 indexlen = Context->vcount + 1;
1589 indexlen = PagerIndexLines;
1591 indicator = indexlen / 3;
1593 if (option (OPTSTATUSONTOP))
1596 statusoffset = IsHeader (extra) ? indexlen : 0;
1597 bodyoffset = statusoffset + 1;
1598 helpoffset = LINES - 2;
1599 bodylen = helpoffset - bodyoffset;
1600 if (!option (OPTHELP))
1607 statusoffset = LINES - 2;
1608 if (!option (OPTHELP))
1610 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1611 bodylen = statusoffset - bodyoffset;
1614 if (option (OPTHELP))
1616 SETCOLOR (MT_COLOR_STATUS);
1617 move (helpoffset, 0);
1618 mutt_paddstr (COLS, helpstr);
1619 SETCOLOR (MT_COLOR_NORMAL);
1622 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1625 if ((SearchCompiled = Resize->SearchCompiled))
1628 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1629 SearchFlag = M_SEARCH;
1630 SearchBack = Resize->SearchBack;
1632 lines = Resize->line;
1633 redraw |= REDRAW_SIGWINCH;
1639 if (IsHeader (extra) && PagerIndexLines)
1643 /* only allocate the space if/when we need the index.
1644 Initialise the menu as per the main index */
1645 index = mutt_new_menu();
1646 index->menu = MENU_MAIN;
1647 index->make_entry = index_make_entry;
1648 index->color = index_color;
1649 index->max = Context->vcount;
1650 index->current = extra->hdr->virtual;
1653 SETCOLOR (MT_COLOR_NORMAL);
1654 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1656 index->pagelen = indexlen - 1;
1658 /* some fudge to work out where abouts the indicator should go */
1659 if (index->current - indicator < 0)
1661 else if (index->max - index->current < index->pagelen - indicator)
1662 index->top = index->max - index->pagelen;
1664 index->top = index->current - indicator;
1666 menu_redraw_index(index);
1669 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1673 if (redraw & REDRAW_SIGWINCH)
1677 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1678 has_types | SearchFlag, &QuoteList, &q_level, &force_redraw,
1680 if (!lineInfo[i].continuation && ++j == lines)
1688 if ((redraw & REDRAW_BODY) || topline != oldtopline)
1691 move (bodyoffset, 0);
1692 curline = oldtopline = topline;
1696 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1)
1698 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1700 (flags & M_DISPLAYFLAGS) | hideQuoted | SearchFlag,
1701 &QuoteList, &q_level, &force_redraw, &SearchRE) > 0)
1705 last_offset = lineInfo[curline].offset;
1706 } while (force_redraw);
1708 SETCOLOR (MT_COLOR_TILDE);
1709 BKGDSET (MT_COLOR_TILDE);
1710 while (lines < bodylen)
1713 if (option (OPTTILDE))
1718 /* We are going to update the pager status bar, so it isn't
1719 * necessary to reset to normal color now. */
1721 redraw |= REDRAW_STATUS; /* need to update the % seen */
1724 if (redraw & REDRAW_STATUS)
1726 /* print out the pager status bar */
1727 SETCOLOR (MT_COLOR_STATUS);
1728 BKGDSET (MT_COLOR_STATUS);
1729 CLEARLINE (statusoffset);
1730 if (IsHeader (extra))
1732 _mutt_make_string (buffer,
1733 COLS-9 < sizeof (buffer) ? COLS-9 : sizeof (buffer),
1734 NONULL (PagerFmt), Context, extra->hdr, M_FORMAT_MAKEPRINT);
1736 else if (IsMsgAttach (extra))
1738 _mutt_make_string (buffer,
1739 COLS - 9 < sizeof (buffer) ? COLS - 9: sizeof (buffer),
1740 NONULL (PagerFmt), Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1742 mutt_paddstr (COLS-10, IsHeader (extra) || IsMsgAttach (extra) ?
1745 if (last_pos < sb.st_size - 1)
1746 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1748 addstr (topline == 0 ? "all)" : "end)");
1749 BKGDSET (MT_COLOR_NORMAL);
1750 SETCOLOR (MT_COLOR_NORMAL);
1753 if ((redraw & REDRAW_INDEX) && index)
1755 /* redraw the pager_index indicator, because the
1756 * flags for this message might have changed. */
1757 menu_redraw_current (index);
1759 /* print out the index status bar */
1760 menu_status_line (buffer, sizeof (buffer), index, NONULL(Status));
1762 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), 0);
1763 SETCOLOR (MT_COLOR_STATUS);
1764 mutt_paddstr (COLS, buffer);
1765 SETCOLOR (MT_COLOR_NORMAL);
1770 move (statusoffset, COLS-1);
1772 ch = km_dokey (MENU_PAGER);
1774 mutt_clear_error ();
1782 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1785 mutt_resize_screen ();
1787 /* Store current position. */
1789 for (i = 0; i <= topline; i++)
1790 if (!lineInfo[i].continuation)
1793 if (flags & M_PAGER_RETWINCH)
1795 Resize = safe_malloc (sizeof (struct resize));
1797 Resize->line = lines;
1798 Resize->SearchCompiled = SearchCompiled;
1799 Resize->SearchBack = SearchBack;
1802 rc = OP_REFORMAT_WINCH;
1806 for (i = 0; i < maxLine; i++)
1808 lineInfo[i].offset = 0;
1809 lineInfo[i].type = -1;
1810 lineInfo[i].continuation = 0;
1811 lineInfo[i].chunks = 0;
1812 lineInfo[i].search_cnt = -1;
1813 lineInfo[i].quote = NULL;
1815 safe_realloc (&(lineInfo[i].syntax),
1816 sizeof (struct syntax_t));
1817 if (SearchCompiled && lineInfo[i].search)
1818 FREE (&(lineInfo[i].search));
1824 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1829 clearok(stdscr,TRUE);/*force complete redraw*/
1849 if (lineInfo[curline].offset < sb.st_size-1)
1851 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1853 else if (option (OPTPAGERSTOP))
1855 /* emulate "less -q" and don't go on to the next message. */
1856 mutt_error _("Bottom of message is shown.");
1860 /* end of the current message, so display the next message. */
1861 rc = OP_MAIN_NEXT_UNDELETED;
1869 topline = upNLines (bodylen-PagerContext, lineInfo, topline, hideQuoted);
1872 mutt_error _("Top of message is shown.");
1876 if (lineInfo[curline].offset < sb.st_size-1)
1881 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1887 mutt_error _("Bottom of message is shown.");
1892 topline = upNLines (1, lineInfo, topline, hideQuoted);
1894 mutt_error _("Top of message is shown.");
1901 mutt_error _("Top of message is shown.");
1906 topline = upNLines (bodylen/2, lineInfo, topline, hideQuoted);
1908 mutt_error _("Top of message is shown.");
1912 if (lineInfo[curline].offset < sb.st_size-1)
1914 topline = upNLines (bodylen/2, lineInfo, curline, hideQuoted);
1916 else if (option (OPTPAGERSTOP))
1918 /* emulate "less -q" and don't go on to the next message. */
1919 mutt_error _("Bottom of message is shown.");
1923 /* end of the current message, so display the next message. */
1924 rc = OP_MAIN_NEXT_UNDELETED;
1929 case OP_SEARCH_NEXT:
1930 case OP_SEARCH_OPPOSITE:
1934 if ((!SearchBack && ch==OP_SEARCH_NEXT) ||
1935 (SearchBack &&ch==OP_SEARCH_OPPOSITE))
1937 /* searching forward */
1938 for (i = topline + 1; i < lastLine; i++)
1940 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1941 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1948 mutt_error _("Not found.");
1952 /* searching backward */
1953 for (i = topline - 1; i >= 0; i--)
1955 if ((!hideQuoted || (has_types &&
1956 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1957 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1964 mutt_error _("Not found.");
1967 if (lineInfo[topline].search_cnt > 0)
1968 SearchFlag = M_SEARCH;
1972 /* no previous search pattern, so fall through to search */
1975 case OP_SEARCH_REVERSE:
1976 strfcpy (buffer, searchbuf, sizeof (buffer));
1977 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1978 _("Search: ")), buffer, sizeof (buffer),
1982 if (!strcmp (buffer, searchbuf))
1986 /* do an implicit search-next */
1987 if (ch == OP_SEARCH)
1988 ch = OP_SEARCH_NEXT;
1990 ch = OP_SEARCH_OPPOSITE;
1999 strfcpy (searchbuf, buffer, sizeof (searchbuf));
2001 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
2002 if (ch == OP_SEARCH)
2004 else if (ch == OP_SEARCH_REVERSE)
2009 regfree (&SearchRE);
2010 for (i = 0; i < lastLine; i++)
2012 if (lineInfo[i].search)
2013 FREE (&(lineInfo[i].search));
2014 lineInfo[i].search_cnt = -1;
2018 if ((err = REGCOMP (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf))) != 0)
2020 regerror (err, &SearchRE, buffer, sizeof (buffer));
2021 mutt_error ("%s", buffer);
2022 regfree (&SearchRE);
2023 for (i = 0; i < maxLine ; i++)
2026 if (lineInfo[i].search)
2027 FREE (&(lineInfo[i].search));
2028 lineInfo[i].search_cnt = -1;
2036 /* update the search pointers */
2038 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2039 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
2040 &QuoteList, &q_level,
2041 &force_redraw, &SearchRE) == 0)
2046 /* searching forward */
2047 for (i = topline; i < lastLine; i++)
2049 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2050 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2054 if (i < lastLine) topline = i;
2058 /* searching backward */
2059 for (i = topline; i >= 0; i--)
2061 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2062 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2066 if (i >= 0) topline = i;
2069 if (lineInfo[topline].search_cnt == 0)
2072 mutt_error _("Not found.");
2075 SearchFlag = M_SEARCH;
2077 redraw = REDRAW_BODY;
2080 case OP_SEARCH_TOGGLE:
2083 SearchFlag ^= M_SEARCH;
2084 redraw = REDRAW_BODY;
2089 /* don't let the user enter the help-menu from the help screen! */
2093 mutt_help (MENU_PAGER);
2094 redraw = REDRAW_FULL;
2098 mutt_error _("Help is currently being shown.");
2101 case OP_PAGER_HIDE_QUOTED:
2104 hideQuoted ^= M_HIDE;
2105 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
2106 topline = upNLines (1, lineInfo, topline, hideQuoted);
2108 redraw = REDRAW_BODY;
2112 case OP_PAGER_SKIP_QUOTED:
2116 int new_topline = topline;
2118 while ((new_topline < lastLine ||
2119 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2120 new_topline, &lastLine, &maxLine, M_TYPES,
2121 &QuoteList, &q_level, &force_redraw, &SearchRE))))
2122 && lineInfo[new_topline].type != MT_COLOR_QUOTED)
2127 mutt_error _("No more quoted text.");
2131 while ((new_topline < lastLine ||
2132 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2133 new_topline, &lastLine, &maxLine, M_TYPES,
2134 &QuoteList, &q_level, &force_redraw, &SearchRE))))
2135 && lineInfo[new_topline].type == MT_COLOR_QUOTED)
2140 mutt_error _("No more unquoted text after quoted text.");
2143 topline = new_topline;
2147 case OP_PAGER_BOTTOM: /* move to the end of the file */
2148 if (lineInfo[curline].offset < sb.st_size - 1)
2151 /* make sure the types are defined to the end of file */
2152 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2153 &maxLine, has_types,
2154 &QuoteList, &q_level, &force_redraw,
2157 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2160 mutt_error _("Bottom of message is shown.");
2164 clearok (stdscr, TRUE);
2165 redraw = REDRAW_FULL;
2169 km_error_key (MENU_PAGER);
2172 /* --------------------------------------------------------------------
2173 * The following are operations on the current message rather than
2174 * adjusting the view of the message.
2177 case OP_BOUNCE_MESSAGE:
2178 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra))
2180 if (IsMsgAttach (extra))
2181 mutt_attach_bounce (extra->fp, extra->hdr,
2182 extra->idx, extra->idxlen,
2185 ci_bounce_message (extra->hdr, &redraw);
2189 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra))
2191 if (IsMsgAttach (extra))
2192 mutt_attach_resend (extra->fp, extra->hdr,
2193 extra->idx, extra->idxlen,
2196 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2197 redraw = REDRAW_FULL;
2200 case OP_CREATE_ALIAS:
2201 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2202 if (IsMsgAttach (extra))
2203 mutt_create_alias (extra->bdy->hdr->env, NULL);
2205 mutt_create_alias (extra->hdr->env, NULL);
2206 MAYBE_REDRAW (redraw);
2209 case OP_PURGE_MESSAGE:
2211 CHECK_MODE(IsHeader (extra));
2215 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2218 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2219 mutt_set_flag (Context, extra->hdr, M_PURGED,
2220 ch != OP_PURGE_MESSAGE ? 0 : 1);
2221 if (option (OPTDELETEUNTAG))
2222 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2223 redraw = REDRAW_STATUS | REDRAW_INDEX;
2224 if (option (OPTRESOLVE))
2227 rc = OP_MAIN_NEXT_UNDELETED;
2231 case OP_DELETE_THREAD:
2232 case OP_DELETE_SUBTHREAD:
2233 CHECK_MODE(IsHeader (extra));
2237 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2240 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2241 ch == OP_DELETE_THREAD ? 0 : 1);
2245 if (option (OPTDELETEUNTAG))
2246 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2247 ch == OP_DELETE_THREAD ? 0 : 1);
2248 if (option (OPTRESOLVE))
2250 rc = OP_MAIN_NEXT_UNDELETED;
2254 if (!option (OPTRESOLVE) && PagerIndexLines)
2255 redraw = REDRAW_FULL;
2257 redraw = REDRAW_STATUS | REDRAW_INDEX;
2261 case OP_DISPLAY_ADDRESS:
2262 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2263 if (IsMsgAttach (extra))
2264 mutt_display_address (extra->bdy->hdr->env);
2266 mutt_display_address (extra->hdr->env);
2269 case OP_ENTER_COMMAND:
2270 old_smart_wrap = option (OPTWRAP);
2271 old_markers = option (OPTMARKERS);
2272 old_PagerIndexLines = PagerIndexLines;
2274 CurrentMenu = MENU_PAGER;
2275 mutt_enter_command ();
2277 if (option (OPTNEEDRESORT))
2279 unset_option (OPTNEEDRESORT);
2280 CHECK_MODE(IsHeader (extra));
2281 set_option (OPTNEEDRESORT);
2284 if (old_PagerIndexLines != PagerIndexLines)
2287 mutt_menuDestroy (&index);
2291 if (option (OPTWRAP) != old_smart_wrap ||
2292 option (OPTMARKERS) != old_markers)
2294 if (flags & M_PAGER_RETWINCH)
2297 rc = OP_REFORMAT_WINCH;
2301 /* count the real lines above */
2303 for (i = 0; i <= topline; i++)
2305 if (!lineInfo[i].continuation)
2309 /* we need to restart the whole thing */
2310 for (i = 0; i < maxLine; i++)
2312 lineInfo[i].offset = 0;
2313 lineInfo[i].type = -1;
2314 lineInfo[i].continuation = 0;
2315 lineInfo[i].chunks = 0;
2316 lineInfo[i].search_cnt = -1;
2317 lineInfo[i].quote = NULL;
2319 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2320 if (SearchCompiled && lineInfo[i].search)
2321 FREE (&(lineInfo[i].search));
2326 regfree (&SearchRE);
2331 /* try to keep the old position */
2334 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2335 &lastLine, &maxLine,
2336 (has_types ? M_TYPES : 0),
2337 &QuoteList, &q_level, &force_redraw,
2340 if (! lineInfo[topline].continuation)
2349 if (option (OPTFORCEREDRAWPAGER))
2350 redraw = REDRAW_FULL;
2351 unset_option (OPTFORCEREDRAWINDEX);
2352 unset_option (OPTFORCEREDRAWPAGER);
2355 case OP_FLAG_MESSAGE:
2356 CHECK_MODE(IsHeader (extra));
2360 if (Context->magic == M_POP)
2363 mutt_error _("Can't change 'important' flag on POP server.");
2369 CHECK_IMAP_ACL(IMAP_ACL_WRITE);
2373 if (Context->magic == M_NNTP)
2376 mutt_error _("Can't change 'important' flag on NNTP server.");
2381 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2382 redraw = REDRAW_STATUS | REDRAW_INDEX;
2383 if (option (OPTRESOLVE))
2386 rc = OP_MAIN_NEXT_UNDELETED;
2391 CHECK_MODE(IsHeader (extra) || IsAttach (extra));
2392 if (IsAttach (extra))
2393 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2395 mutt_pipe_message (extra->hdr);
2396 MAYBE_REDRAW (redraw);
2400 CHECK_MODE(IsHeader (extra) || IsAttach (extra));
2401 if (IsAttach (extra))
2402 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2404 mutt_print_message (extra->hdr);
2408 CHECK_MODE(IsHeader (extra) && !IsAttach (extra));
2410 ci_send_message (0, NULL, NULL, extra->ctx, extra->hdr);
2411 redraw = REDRAW_FULL;
2416 CHECK_MODE(IsHeader (extra) && !IsAttach (extra));
2418 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2419 !((NNTP_DATA *)extra->ctx->data)->allowed &&
2420 query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
2422 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2423 redraw = REDRAW_FULL;
2426 case OP_FORWARD_TO_GROUP:
2427 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2429 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2430 !((NNTP_DATA *)extra->ctx->data)->allowed &&
2431 query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
2433 if (IsMsgAttach (extra))
2434 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2435 extra->idxlen, extra->bdy, SENDNEWS);
2437 ci_send_message (SENDNEWS|SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2438 redraw = REDRAW_FULL;
2442 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2445 if (IsMsgAttach (extra))
2446 followup_to = extra->bdy->hdr->env->followup_to;
2448 followup_to = extra->hdr->env->followup_to;
2450 if (!followup_to || mutt_strcasecmp (followup_to, "poster") ||
2451 query_quadoption (OPT_FOLLOWUPTOPOSTER,_("Reply by mail as poster prefers?")) != M_YES)
2453 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2454 !((NNTP_DATA *)extra->ctx->data)->allowed &&
2455 query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
2457 if (IsMsgAttach (extra))
2458 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2459 extra->idxlen, extra->bdy, SENDNEWS|SENDREPLY);
2461 ci_send_message (SENDNEWS|SENDREPLY, NULL, NULL,
2462 extra->ctx, extra->hdr);
2463 redraw = REDRAW_FULL;
2469 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2471 if (IsMsgAttach (extra))
2472 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2473 extra->idxlen, extra->bdy,
2476 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2477 redraw = REDRAW_FULL;
2480 case OP_RECALL_MESSAGE:
2481 CHECK_MODE(IsHeader (extra));
2483 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2484 redraw = REDRAW_FULL;
2487 case OP_GROUP_REPLY:
2488 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2490 if (IsMsgAttach (extra))
2491 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2492 extra->idxlen, extra->bdy, SENDREPLY|SENDGROUPREPLY);
2494 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx, extra->hdr);
2495 redraw = REDRAW_FULL;
2499 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2501 if (IsMsgAttach (extra))
2502 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2503 extra->idxlen, extra->bdy, SENDREPLY|SENDLISTREPLY);
2505 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx, extra->hdr);
2506 redraw = REDRAW_FULL;
2509 case OP_FORWARD_MESSAGE:
2510 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2512 if (IsMsgAttach (extra))
2513 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2514 extra->idxlen, extra->bdy, 0);
2516 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2517 redraw = REDRAW_FULL;
2520 case OP_DECRYPT_SAVE:
2528 if (IsAttach (extra))
2530 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr, NULL);
2534 case OP_COPY_MESSAGE:
2535 case OP_DECODE_SAVE:
2536 case OP_DECODE_COPY:
2537 case OP_DECRYPT_COPY:
2538 if (!WithCrypto && ch == OP_DECRYPT_COPY)
2543 CHECK_MODE(IsHeader (extra));
2544 if (mutt_save_message (extra->hdr,
2545 (ch == OP_DECRYPT_SAVE) ||
2546 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2547 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2548 (ch == OP_DECRYPT_SAVE) || (ch == OP_DECRYPT_COPY) ||
2550 &redraw) == 0 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2551 || ch == OP_DECRYPT_SAVE
2554 if (option (OPTRESOLVE))
2557 rc = OP_MAIN_NEXT_UNDELETED;
2560 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2562 MAYBE_REDRAW (redraw);
2565 case OP_SHELL_ESCAPE:
2566 mutt_shell_escape ();
2567 MAYBE_REDRAW (redraw);
2571 CHECK_MODE(IsHeader (extra));
2572 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2573 redraw = REDRAW_STATUS | REDRAW_INDEX;
2574 if (option (OPTRESOLVE))
2582 CHECK_MODE(IsHeader (extra));
2586 CHECK_IMAP_ACL(IMAP_ACL_SEEN);
2589 if (extra->hdr->read || extra->hdr->old)
2590 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2592 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2594 Context->msgnotreadyet = -1;
2595 redraw = REDRAW_STATUS | REDRAW_INDEX;
2596 if (option (OPTRESOLVE))
2599 rc = OP_MAIN_NEXT_UNDELETED;
2604 CHECK_MODE(IsHeader (extra));
2608 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2611 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2612 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2613 redraw = REDRAW_STATUS | REDRAW_INDEX;
2614 if (option (OPTRESOLVE))
2621 case OP_UNDELETE_THREAD:
2622 case OP_UNDELETE_SUBTHREAD:
2623 CHECK_MODE(IsHeader (extra));
2627 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2630 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2631 ch == OP_UNDELETE_THREAD ? 0 : 1)
2632 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2633 ch == OP_UNDELETE_THREAD ? 0 : 1);
2637 if (option (OPTRESOLVE))
2639 rc = (ch == OP_DELETE_THREAD) ?
2640 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2644 if (!option (OPTRESOLVE) && PagerIndexLines)
2645 redraw = REDRAW_FULL;
2647 redraw = REDRAW_STATUS | REDRAW_INDEX;
2659 case OP_VIEW_ATTACHMENTS:
2660 if (flags & M_PAGER_ATTACHMENT)
2663 rc = OP_ATTACH_COLLAPSE;
2666 CHECK_MODE(IsHeader (extra));
2667 mutt_view_attachments (extra->hdr);
2668 if (extra->hdr->attach_del)
2669 Context->changed = 1;
2670 redraw = REDRAW_FULL;
2675 if (!(WithCrypto & APPLICATION_PGP))
2680 CHECK_MODE(IsHeader(extra));
2682 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2683 redraw = REDRAW_FULL;
2687 case OP_FORGET_PASSPHRASE:
2688 crypt_forget_passphrase ();
2691 case OP_EXTRACT_KEYS:
2697 CHECK_MODE(IsHeader(extra));
2698 crypt_extract_keys_from_messages(extra->hdr);
2699 redraw = REDRAW_FULL;
2709 if (IsHeader (extra))
2710 Context->msgnotreadyet = -1;
2712 cleanup_quote (&QuoteList);
2714 for (i = 0; i < maxLine ; i++)
2716 FREE (&(lineInfo[i].syntax));
2717 if (SearchCompiled && lineInfo[i].search)
2718 FREE (&(lineInfo[i].search));
2722 regfree (&SearchRE);
2727 mutt_menuDestroy(&index);
2728 return (rc != -1 ? rc : 0);