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"
34 #include "imap_private.h"
37 #include "mutt_crypt.h"
46 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
48 #define IsAttach(x) (x && (x)->bdy)
49 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
50 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
51 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
52 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
54 static const char *Not_available_in_this_menu = N_("Not available in this menu.");
55 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
56 static const char *Function_not_permitted_in_attach_message_mode = N_("Function not permitted in attach-message mode.");
58 #define CHECK_MODE(x) if (!(x)) \
61 mutt_error _(Not_available_in_this_menu); \
65 #define CHECK_READONLY if (Context->readonly) \
68 mutt_error _(Mailbox_is_read_only); \
72 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
75 mutt_error _(Function_not_permitted_in_attach_message_mode); \
80 /* the error message returned here could be better. */
81 #define CHECK_IMAP_ACL(aclbit) if (Context->magic == M_IMAP) \
82 if (mutt_bit_isset (((IMAP_DATA *)Context->data)->capabilities, ACL) \
83 && !mutt_bit_isset(((IMAP_DATA *)Context->data)->rights,aclbit)){ \
85 mutt_error ("Operation not permitted by the IMAP ACL for this mailbox"); \
96 struct q_class_t *next, *prev;
97 struct q_class_t *down, *up;
114 struct syntax_t *syntax;
115 struct syntax_t *search;
116 struct q_class_t *quote;
119 #define ANSI_OFF (1<<0)
120 #define ANSI_BLINK (1<<1)
121 #define ANSI_BOLD (1<<2)
122 #define ANSI_UNDERLINE (1<<3)
123 #define ANSI_REVERSE (1<<4)
124 #define ANSI_COLOR (1<<5)
126 typedef struct _ansi_attr {
133 static short InHelp = 0;
135 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
136 static struct resize {
143 #define NumSigLines 4
145 static int check_sig (const char *s, struct line_t *info, int n)
149 while (n > 0 && count <= NumSigLines)
151 if (info[n].type != MT_COLOR_SIGNATURE)
160 if (count > NumSigLines)
162 /* check for a blank line */
177 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags, int special,
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)
190 if (!cnt && option (OPTMARKERS))
192 SETCOLOR (MT_COLOR_MARKERS);
194 last_color = ColorDefs[MT_COLOR_MARKERS];
196 m = (lineInfo[n].syntax)[0].first;
197 cnt += (lineInfo[n].syntax)[0].last;
201 if (!(flags & M_SHOWCOLOR))
202 def_color = ColorDefs[MT_COLOR_NORMAL];
203 else if (lineInfo[m].type == MT_COLOR_HEADER)
204 def_color = (lineInfo[m].syntax)[0].color;
206 def_color = ColorDefs[lineInfo[m].type];
208 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED)
210 struct q_class_t *class = lineInfo[m].quote;
214 def_color = class->color;
216 while (class && class->length > cnt)
218 def_color = class->color;
225 if (flags & M_SHOWCOLOR)
227 for (i = 0; i < lineInfo[m].chunks; i++)
229 /* we assume the chunks are sorted */
230 if (cnt > (lineInfo[m].syntax)[i].last)
232 if (cnt < (lineInfo[m].syntax)[i].first)
234 if (cnt != (lineInfo[m].syntax)[i].last)
236 color = (lineInfo[m].syntax)[i].color;
239 /* don't break here, as cnt might be
240 * in the next chunk as well */
244 if (flags & M_SEARCH)
246 for (i = 0; i < lineInfo[m].search_cnt; i++)
248 if (cnt > (lineInfo[m].search)[i].last)
250 if (cnt < (lineInfo[m].search)[i].first)
252 if (cnt != (lineInfo[m].search)[i].last)
254 color = ColorDefs[MT_COLOR_SEARCH];
261 /* handle "special" bold & underlined characters */
262 if (special || a->attr)
265 if ((a->attr & ANSI_COLOR))
268 a->pair = mutt_alloc_color (a->fg, a->bg);
270 if (a->attr & ANSI_BOLD)
275 if ((special & A_BOLD) || (a->attr & ANSI_BOLD))
277 if (ColorDefs[MT_COLOR_BOLD] && !search)
278 color = ColorDefs[MT_COLOR_BOLD];
282 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE))
284 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
285 color = ColorDefs[MT_COLOR_UNDERLINE];
287 color ^= A_UNDERLINE;
289 else if (a->attr & ANSI_REVERSE)
293 else if (a->attr & ANSI_BLINK)
297 else if (a->attr & ANSI_OFF)
303 if (color != last_color)
311 append_line (struct line_t *lineInfo, int n, int cnt)
315 lineInfo[n+1].type = lineInfo[n].type;
316 (lineInfo[n+1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
317 lineInfo[n+1].continuation = 1;
319 /* find the real start of the line */
320 for (m = n; m >= 0; m--)
321 if (lineInfo[m].continuation == 0) break;
323 (lineInfo[n+1].syntax)[0].first = m;
324 (lineInfo[n+1].syntax)[0].last = (lineInfo[n].continuation) ?
325 cnt + (lineInfo[n].syntax)[0].last : cnt;
329 new_class_color (struct q_class_t *class, int *q_level)
331 class->index = (*q_level)++;
332 class->color = ColorQuote[class->index % ColorQuoteUsed];
336 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
337 int index, int *q_level)
339 struct q_class_t * q_list;
342 new_class->index = -1;
346 if (q_list->index >= index)
349 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
352 q_list = q_list->down;
353 else if (q_list->next)
354 q_list = q_list->next;
357 while (!q_list->next)
364 q_list = q_list->next;
368 new_class->index = index;
369 new_class->color = ColorQuote[index % ColorQuoteUsed];
374 cleanup_quote (struct q_class_t **QuoteList)
376 struct q_class_t *ptr;
380 if ((*QuoteList)->down)
381 cleanup_quote (&((*QuoteList)->down));
382 ptr = (*QuoteList)->next;
383 if ((*QuoteList)->prefix)
384 FREE (&(*QuoteList)->prefix);
392 static struct q_class_t *
393 classify_quote (struct q_class_t **QuoteList, const char *qptr,
394 int length, int *force_redraw, int *q_level)
396 struct q_class_t *q_list = *QuoteList;
397 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
399 int offset, tail_lng;
402 if (ColorQuoteUsed <= 1)
404 /* not much point in classifying quotes... */
406 if (*QuoteList == NULL)
408 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
409 class->color = ColorQuote[0];
415 /* Did I mention how much I like emulating Lisp in C? */
417 /* classify quoting prefix */
420 if (length <= q_list->length)
422 /* case 1: check the top level nodes */
424 if (mutt_strncmp (qptr, q_list->prefix, length) == 0)
426 if (length == q_list->length)
427 return q_list; /* same prefix: return the current class */
429 /* found shorter prefix */
432 /* add a node above q_list */
433 tmp = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
434 tmp->prefix = (char *) safe_calloc (1, length + 1);
435 strncpy (tmp->prefix, qptr, length);
436 tmp->length = length;
438 /* replace q_list by tmp in the top level list */
441 tmp->next = q_list->next;
442 q_list->next->prev = tmp;
446 tmp->prev = q_list->prev;
447 q_list->prev->next = tmp;
450 /* make q_list a child of tmp */
454 /* q_list has no siblings for now */
458 /* update the root if necessary */
459 if (q_list == *QuoteList)
462 index = q_list->index;
464 /* tmp should be the return class too */
467 /* next class to test; if tmp is a shorter prefix for another
468 * node, that node can only be in the top level list, so don't
469 * go down after this point
475 /* found another branch for which tmp is a shorter prefix */
477 /* save the next sibling for later */
480 /* unlink q_list from the top level list */
482 q_list->next->prev = q_list->prev;
484 q_list->prev->next = q_list->next;
486 /* at this point, we have a tmp->down; link q_list to it */
488 /* sibling order is important here, q_list should be linked last */
496 index = q_list->index;
498 /* next class to test; as above, we shouldn't go down */
502 /* we found a shorter prefix, so certain quotes have changed classes */
508 /* shorter, but not a substring of the current class: try next */
509 q_list = q_list->next;
515 /* case 2: try subclassing the current top level node */
517 /* tmp != NULL means we already found a shorter prefix at case 1 */
518 if (tmp == NULL && mutt_strncmp (qptr, q_list->prefix, q_list->length) == 0)
520 /* ok, it's a subclass somewhere on this branch */
523 offset = q_list->length;
525 q_list = q_list->down;
526 tail_lng = length - offset;
527 tail_qptr = (char *) qptr + offset;
531 if (length <= q_list->length)
533 if (mutt_strncmp (tail_qptr, (q_list->prefix) + offset, tail_lng) == 0)
535 /* same prefix: return the current class */
536 if (length == q_list->length)
539 /* found shorter common prefix */
542 /* add a node above q_list */
543 tmp = (struct q_class_t *) safe_calloc (1,
544 sizeof (struct q_class_t));
545 tmp->prefix = (char *) safe_calloc (1, length + 1);
546 strncpy (tmp->prefix, qptr, length);
547 tmp->length = length;
549 /* replace q_list by tmp */
552 tmp->next = q_list->next;
553 q_list->next->prev = tmp;
557 tmp->prev = q_list->prev;
558 q_list->prev->next = tmp;
561 /* make q_list a child of tmp */
563 tmp->up = q_list->up;
565 if (tmp->up->down == q_list)
568 /* q_list has no siblings */
572 index = q_list->index;
574 /* tmp should be the return class too */
577 /* next class to test */
582 /* found another branch for which tmp is a shorter prefix */
584 /* save the next sibling for later */
587 /* unlink q_list from the top level list */
589 q_list->next->prev = q_list->prev;
591 q_list->prev->next = q_list->next;
593 /* at this point, we have a tmp->down; link q_list to it */
602 index = q_list->index;
604 /* next class to test */
608 /* we found a shorter prefix, so we need a redraw */
614 q_list = q_list->next;
620 /* longer than the current prefix: try subclassing it */
621 if (tmp == NULL && mutt_strncmp (tail_qptr, (q_list->prefix) + offset,
622 q_list->length - offset) == 0)
624 /* still a subclass: go down one level */
626 offset = q_list->length;
628 q_list = q_list->down;
629 tail_lng = length - offset;
630 tail_qptr = (char *) qptr + offset;
636 /* nope, try the next prefix */
637 q_list = q_list->next;
643 /* still not found so far: add it as a sibling to the current node */
646 tmp = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
647 tmp->prefix = (char *) safe_calloc (1, length + 1);
648 strncpy (tmp->prefix, qptr, length);
649 tmp->length = length;
653 tmp->next = ptr->down;
654 ptr->down->prev = tmp;
659 new_class_color (tmp, q_level);
666 shift_class_colors (*QuoteList, tmp, index, q_level);
673 /* nope, try the next prefix */
674 q_list = q_list->next;
682 /* not found so far: add it as a top level class */
683 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
684 class->prefix = (char *) safe_calloc (1, length + 1);
685 strncpy (class->prefix, qptr, length);
686 class->length = length;
687 new_class_color (class, q_level);
691 class->next = *QuoteList;
692 (*QuoteList)->prev = class;
698 shift_class_colors (*QuoteList, tmp, index, q_level);
703 static int check_attachment_marker (char *);
706 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
707 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
710 COLOR_LINE *color_line;
711 regmatch_t pmatch[1], smatch[1];
712 int found, offset, null_rx, i;
714 if (n == 0 || ISHEADER (lineInfo[n-1].type))
717 lineInfo[n].type = MT_COLOR_NORMAL;
718 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t'))
720 lineInfo[n].type = lineInfo[n-1].type; /* wrapped line */
721 (lineInfo[n].syntax)[0].color = (lineInfo[n-1].syntax)[0].color;
725 lineInfo[n].type = MT_COLOR_HDEFAULT;
726 color_line = ColorHdrList;
729 if (REGEXEC (color_line->rx, buf) == 0)
731 lineInfo[n].type = MT_COLOR_HEADER;
732 lineInfo[n].syntax[0].color = color_line->pair;
735 color_line = color_line->next;
739 else if (mutt_strncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
740 lineInfo[n].type = MT_COLOR_NORMAL;
742 else if (mutt_strncmp ("[-- ", buf, 4) == 0)
743 lineInfo[n].type = MT_COLOR_ATTACHMENT;
745 else if (check_attachment_marker ((char *) raw) == 0)
746 lineInfo[n].type = MT_COLOR_ATTACHMENT;
748 else if (mutt_strcmp ("-- \n", buf) == 0 || mutt_strcmp ("-- \r\n", buf) == 0)
752 lineInfo[n].type = MT_COLOR_SIGNATURE;
753 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
754 (lineInfo[i].type == MT_COLOR_NORMAL ||
755 lineInfo[i].type == MT_COLOR_QUOTED ||
756 lineInfo[i].type == MT_COLOR_HEADER))
759 if (lineInfo[i].chunks)
761 lineInfo[i].chunks = 0;
762 safe_realloc (&(lineInfo[n].syntax),
763 sizeof (struct syntax_t));
765 lineInfo[i++].type = MT_COLOR_SIGNATURE;
768 else if (check_sig (buf, lineInfo, n - 1) == 0)
769 lineInfo[n].type = MT_COLOR_SIGNATURE;
770 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0)
772 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0)
774 if (smatch[0].rm_so > 0)
778 /* hack to avoid making an extra copy of buf */
779 c = buf[smatch[0].rm_so];
780 buf[smatch[0].rm_so] = 0;
782 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0)
784 if (q_classify && lineInfo[n].quote == NULL)
785 lineInfo[n].quote = classify_quote (QuoteList,
786 buf + pmatch[0].rm_so,
787 pmatch[0].rm_eo - pmatch[0].rm_so,
788 force_redraw, q_level);
789 lineInfo[n].type = MT_COLOR_QUOTED;
792 lineInfo[n].type = MT_COLOR_NORMAL;
794 buf[smatch[0].rm_so] = c;
797 lineInfo[n].type = MT_COLOR_NORMAL;
801 if (q_classify && lineInfo[n].quote == NULL)
802 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
803 pmatch[0].rm_eo - pmatch[0].rm_so,
804 force_redraw, q_level);
805 lineInfo[n].type = MT_COLOR_QUOTED;
809 lineInfo[n].type = MT_COLOR_NORMAL;
812 if (lineInfo[n].type == MT_COLOR_NORMAL ||
813 lineInfo[n].type == MT_COLOR_QUOTED)
818 lineInfo[n].chunks = 0;
826 color_line = ColorBodyList;
829 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
830 (offset ? REG_NOTBOL : 0)) == 0)
832 if (pmatch[0].rm_eo != pmatch[0].rm_so)
836 if (++(lineInfo[n].chunks) > 1)
837 safe_realloc (&(lineInfo[n].syntax),
838 (lineInfo[n].chunks) * sizeof (struct syntax_t));
840 i = lineInfo[n].chunks - 1;
841 pmatch[0].rm_so += offset;
842 pmatch[0].rm_eo += offset;
844 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
845 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
846 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last))
848 (lineInfo[n].syntax)[i].color = color_line->pair;
849 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
850 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
856 null_rx = 1; /* empty regexp; don't add it, but keep looking */
858 color_line = color_line->next;
862 offset++; /* avoid degenerate cases */
864 offset = (lineInfo[n].syntax)[i].last;
865 } while (found || null_rx);
869 static int is_ansi (unsigned char *buf)
871 while (*buf && (isdigit(*buf) || *buf == ';'))
873 return (*buf == 'm');
876 static int check_attachment_marker (char *p)
878 char *q = AttachmentMarker;
880 for (;*p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++)
882 return (int) (*p - *q);
885 static int grok_ansi(unsigned char *buf, int pos, ansi_attr *a)
889 while (isdigit(buf[x]) || buf[x] == ';')
892 /* Character Attributes */
893 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm')
899 mutt_free_color (a->fg, a->bg);
906 if (buf[pos] == '1' && (pos+1 == x || buf[pos+1] == ';'))
908 a->attr |= ANSI_BOLD;
911 else if (buf[pos] == '4' && (pos+1 == x || buf[pos+1] == ';'))
913 a->attr |= ANSI_UNDERLINE;
916 else if (buf[pos] == '5' && (pos+1 == x || buf[pos+1] == ';'))
918 a->attr |= ANSI_BLINK;
921 else if (buf[pos] == '7' && (pos+1 == x || buf[pos+1] == ';'))
923 a->attr |= ANSI_REVERSE;
926 else if (buf[pos] == '0' && (pos+1 == x || buf[pos+1] == ';'))
930 mutt_free_color(a->fg,a->bg);
936 else if (buf[pos] == '3' && isdigit(buf[pos+1]))
940 mutt_free_color(a->fg,a->bg);
943 a->attr |= ANSI_COLOR;
944 a->fg = buf[pos+1] - '0';
947 else if (buf[pos] == '4' && isdigit(buf[pos+1]))
951 mutt_free_color(a->fg,a->bg);
954 a->attr |= ANSI_COLOR;
955 a->bg = buf[pos+1] - '0';
960 while (pos < x && buf[pos] != ';') pos++;
970 fill_buffer (FILE *f, long *last_pos, long offset, unsigned char *buf,
971 unsigned char *fmt, size_t blen, int *buf_ready)
979 if (offset != *last_pos)
980 fseek (f, offset, 0);
981 if (fgets ((char *) buf, blen - 1, f) == NULL)
986 *last_pos = ftell (f);
987 b_read = (int) (*last_pos - offset);
990 /* copy "buf" to "fmt", but without bold and underline controls */
994 if (*p == '\010' && (p > buf))
996 if (*(p+1) == '_') /* underline */
998 else if (*(p+1)) /* bold or overstrike */
1006 else if (*p == '\033' && *(p+1) == '[' && is_ansi (p + 2))
1008 while (*p++ != 'm') /* skip ANSI sequence */
1011 else if (*p == '\033' && *(p+1) == ']' && check_attachment_marker ((char *) p) == 0)
1013 dprint (2, (debugfile, "fill_buffer: Seen attachment marker.\n"));
1014 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
1031 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
1032 int flags, ansi_attr *pa, int cnt,
1033 int *pspace, int *pvch, int *pcol, int *pspecial)
1035 int space = -1; /* index of the last space or TAB */
1036 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
1037 int ch, vch, k, last_special = -1, special = 0, t;
1041 int wrap_cols = COLS - WrapMargin - SidebarWidth;
1046 /* FIXME: this should come from lineInfo */
1047 memset(&mbstate, 0, sizeof(mbstate));
1049 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k)
1051 /* Handle ANSI sequences */
1052 while (cnt-ch >= 2 && buf[ch] == '\033' && buf[ch+1] == '[' &&
1054 ch = grok_ansi (buf, ch+2, pa) + 1;
1056 while (cnt-ch >= 2 && buf[ch] == '\033' && buf[ch+1] == ']' &&
1057 check_attachment_marker ((char *) buf+ch) == 0)
1059 while (buf[ch++] != '\a')
1064 /* is anything left to do? */
1068 k = mbrtowc (&wc, (char *)buf+ch, cnt-ch, &mbstate);
1069 if (k == -2 || k == -1)
1071 dprint (1, (debugfile, "%s:%d: mbrtowc returned %d; errno = %d.\n",
1072 __FILE__, __LINE__, k, errno));
1073 if (col + 4 > wrap_cols)
1077 printw ("\\%03o", buf[ch]);
1084 /* Handle backspace */
1092 while ((wc1 = 0, mbstate1 = mbstate,
1093 k1 = k + mbrtowc (&wc1, (char *)buf+ch+k, cnt-ch-k, &mbstate1),
1094 k1 - k > 0 && wc1 == '\b') &&
1096 k2 = mbrtowc (&wc1, (char *)buf+ch+k1, cnt-ch-k1, &mbstate1),
1097 k2 > 0 && IsWPrint (wc1)))
1101 special |= (wc == '_' && special & A_UNDERLINE)
1102 ? A_UNDERLINE : A_BOLD;
1104 else if (wc == '_' || wc1 == '_')
1106 special |= A_UNDERLINE;
1107 wc = (wc1 == '_') ? wc : wc1;
1111 /* special = 0; / * overstrike: nothing to do! */
1121 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1122 special || last_special || pa->attr))
1124 resolve_color (*lineInfo, n, vch, flags, special, pa);
1125 last_special = special;
1133 if (col + t > wrap_cols)
1139 else if (wc == '\n')
1141 else if (wc == '\t')
1148 for (; col < t; col++)
1153 else if (wc < 0x20 || wc == 0x7f)
1155 if (col + 2 > wrap_cols)
1159 printw ("^%c", ('@' + wc) & 0x7f);
1161 else if (wc < 0x100)
1163 if (col + 4 > wrap_cols)
1167 printw ("\\%03o", wc);
1171 if (col + 1 > wrap_cols)
1175 addch (replacement_char ());
1181 *pspecial = special;
1187 * flags M_SHOWFLAT, show characters (used for displaying help)
1188 * M_SHOWCOLOR, show characters in color
1189 * otherwise don't show characters
1190 * M_HIDE, don't show quoted text
1191 * M_SEARCH, resolve search patterns
1192 * M_TYPES, compute line's type
1193 * M_PAGER_NSKIP, keeps leading whitespace
1194 * M_PAGER_MARKER, eventually show markers
1197 * -1 EOF was reached
1198 * 0 normal exit, line was not displayed
1199 * >0 normal exit, line was displayed
1203 display_line (FILE *f, long *last_pos, struct line_t **lineInfo, int n,
1204 int *last, int *max, int flags, struct q_class_t **QuoteList,
1205 int *q_level, int *force_redraw, regex_t *SearchRE)
1207 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1208 unsigned char *buf_ptr = buf;
1209 int ch, vch, col, cnt, b_read;
1210 int buf_ready = 0, change_last = 0;
1215 ansi_attr a = {0,0,0,-1};
1216 regmatch_t pmatch[1];
1226 safe_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1227 for (ch = *last; ch < *max ; ch++)
1229 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1230 (*lineInfo)[ch].type = -1;
1231 (*lineInfo)[ch].search_cnt = -1;
1232 (*lineInfo)[ch].syntax = safe_malloc (sizeof (struct syntax_t));
1233 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last = -1;
1237 /* only do color hiliting if we are viewing a message */
1238 if (flags & (M_SHOWCOLOR | M_TYPES))
1240 if ((*lineInfo)[n].type == -1)
1242 /* determine the line class */
1243 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1250 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1251 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1253 /* avoid race condition for continuation lines when scrolling up */
1254 for (m = n + 1; m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation; m++)
1255 (*lineInfo)[m].type = (*lineInfo)[n].type;
1258 /* this also prevents searching through the hidden lines */
1259 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1260 flags = 0; /* M_NOSHOW */
1263 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1264 * don't want to compute it every time M_TYPES is set, since this
1265 * would slow down the "bottom" function unacceptably. A compromise
1266 * solution is hence to call regexec() again, just to find out the
1267 * length of the quote prefix.
1269 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1270 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1272 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1278 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1279 (*lineInfo)[n].quote = classify_quote (QuoteList,
1280 (char *) fmt + pmatch[0].rm_so,
1281 pmatch[0].rm_eo - pmatch[0].rm_so,
1282 force_redraw, q_level);
1285 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation && (*lineInfo)[n].search_cnt == -1)
1287 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1295 (*lineInfo)[n].search_cnt = 0;
1296 while (regexec (SearchRE, (char *) fmt + offset, 1, pmatch, (offset ? REG_NOTBOL : 0)) == 0)
1298 if (++((*lineInfo)[n].search_cnt) > 1)
1299 safe_realloc (&((*lineInfo)[n].search),
1300 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1302 (*lineInfo)[n].search = safe_malloc (sizeof (struct syntax_t));
1303 pmatch[0].rm_so += offset;
1304 pmatch[0].rm_eo += offset;
1305 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first = pmatch[0].rm_so;
1306 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last = pmatch[0].rm_eo;
1308 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1309 offset++; /* avoid degenerate cases */
1311 offset = pmatch[0].rm_eo;
1317 if (!(flags & M_SHOW) && (*lineInfo)[n+1].offset > 0)
1319 /* we've already scanned this line, so just exit */
1322 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n+1].offset > 0)
1324 /* no need to try to display this line... */
1325 return (1); /* fake display */
1328 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1329 sizeof (buf), &buf_ready)) < 0)
1336 /* now chose a good place to break the line */
1337 cnt = format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col, &special);
1338 buf_ptr = buf + cnt;
1340 /* move the break point only if smart_wrap is set */
1341 if (option (OPTWRAP))
1345 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n' && buf[cnt] != '\r')
1348 /* skip trailing blanks */
1349 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1354 buf_ptr = buf + cnt; /* a very long word... */
1356 if (!(flags & M_PAGER_NSKIP))
1357 /* skip leading blanks on the next line too */
1358 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1362 if (*buf_ptr == '\r')
1364 if (*buf_ptr == '\n')
1367 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n+1].continuation)
1368 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1369 (*lineInfo)[n+1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1371 /* if we don't need to display the line we are done */
1372 if (!(flags & M_SHOW))
1375 /* display the line */
1376 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1378 /* avoid a bug in ncurses... */
1379 #ifndef USE_SLANG_CURSES
1382 SETCOLOR (MT_COLOR_NORMAL);
1387 /* end the last color pattern (needed by S-Lang) */
1388 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1389 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1392 * Fill the blank space at the end of the line with the prevailing color.
1393 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1394 * to make sure to reset the color *after* that
1396 if (flags & M_SHOWCOLOR)
1398 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1399 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1400 def_color = ((*lineInfo)[m].syntax)[0].color;
1402 def_color = ColorDefs[ (*lineInfo)[m].type ];
1404 attrset (def_color);
1406 bkgdset (def_color | ' ');
1410 /* ncurses always wraps lines when you get to the right side of the
1411 * screen, but S-Lang seems to only wrap if the next character is *not*
1414 #ifndef USE_SLANG_CURSES
1420 * reset the color back to normal. This *must* come after the
1421 * addch('\n'), otherwise the color for this line will not be
1422 * filled to the right margin.
1424 if (flags & M_SHOWCOLOR)
1426 SETCOLOR(MT_COLOR_NORMAL);
1427 BKGDSET(MT_COLOR_NORMAL);
1430 /* build a return code */
1431 if (!(flags & M_SHOW))
1438 upNLines (int nlines, struct line_t *info, int cur, int hiding)
1440 while (cur > 0 && nlines > 0)
1443 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1450 static struct mapping_t PagerHelp[] = {
1451 { N_("Exit"), OP_EXIT },
1452 { N_("PrevPg"), OP_PREV_PAGE },
1453 { N_("NextPg"), OP_NEXT_PAGE },
1456 static struct mapping_t PagerHelpExtra[] = {
1457 { N_("View Attachm."), OP_VIEW_ATTACHMENTS },
1458 { N_("Del"), OP_DELETE },
1459 { N_("Reply"), OP_REPLY },
1460 { N_("Next"), OP_MAIN_NEXT_UNDELETED },
1465 static struct mapping_t PagerNewsHelpExtra[] = {
1466 { N_("Post"), OP_POST },
1467 { N_("Followup"), OP_FOLLOWUP },
1468 { N_("Del"), OP_DELETE },
1469 { N_("Next"), OP_MAIN_NEXT_UNDELETED },
1476 /* This pager is actually not so simple as it once was. It now operates in
1477 two modes: one for viewing messages and the other for viewing help. These
1478 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1479 is there so that we can do operations on the current message without the
1480 need to pop back out to the main-menu. */
1482 mutt_pager (const char *banner, const char *fname, int flags, pager_t *extra)
1484 static char searchbuf[STRING];
1485 char buffer[LONG_STRING];
1486 char helpstr[SHORT_STRING*2];
1487 char tmphelp[SHORT_STRING*2];
1488 int maxLine, lastLine = 0;
1489 struct line_t *lineInfo;
1490 struct q_class_t *QuoteList = NULL;
1491 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1492 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1494 int redraw = REDRAW_FULL;
1496 long last_pos = 0, last_offset = 0;
1497 int old_smart_wrap, old_markers;
1500 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1501 int has_types = (IsHeader(extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1503 int bodyoffset = 1; /* offset of first line of real text */
1504 int statusoffset = 0; /* offset for the status bar */
1505 int helpoffset = LINES - 2; /* offset for the help bar. */
1506 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1508 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1509 int indexoffset = 0; /* offset for the PI */
1510 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1511 int indicator = indexlen / 3; /* the indicator line of the PI */
1512 int old_PagerIndexLines; /* some people want to resize it
1513 * while inside the pager... */
1519 if (!(flags & M_SHOWCOLOR))
1520 flags |= M_SHOWFLAT;
1522 if ((fp = fopen (fname, "r")) == NULL)
1524 mutt_perror (fname);
1528 if (stat (fname, &sb) != 0)
1530 mutt_perror (fname);
1536 /* Initialize variables */
1538 if (IsHeader (extra) && !extra->hdr->read)
1540 Context->msgnotreadyet = extra->hdr->msgno;
1541 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1544 lineInfo = safe_malloc (sizeof (struct line_t) * (maxLine = LINES));
1545 for (i = 0 ; i < maxLine ; i++)
1547 memset (&lineInfo[i], 0, sizeof (struct line_t));
1548 lineInfo[i].type = -1;
1549 lineInfo[i].search_cnt = -1;
1550 lineInfo[i].syntax = safe_malloc (sizeof (struct syntax_t));
1551 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1554 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1555 if (IsHeader (extra))
1557 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1558 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1560 (Context && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1563 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1567 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1568 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1569 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1580 if (redraw & REDRAW_FULL)
1582 SETCOLOR (MT_COLOR_NORMAL);
1583 /* clear() doesn't optimize screen redraws */
1587 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1588 indexlen = Context->vcount + 1;
1590 indexlen = PagerIndexLines;
1592 indicator = indexlen / 3;
1594 if (option (OPTSTATUSONTOP))
1597 statusoffset = IsHeader (extra) ? indexlen : 0;
1598 bodyoffset = statusoffset + 1;
1599 helpoffset = LINES - 2;
1600 bodylen = helpoffset - bodyoffset;
1601 if (!option (OPTHELP))
1608 statusoffset = LINES - 2;
1609 if (!option (OPTHELP))
1611 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1612 bodylen = statusoffset - bodyoffset;
1615 if (option (OPTHELP))
1617 SETCOLOR (MT_COLOR_STATUS);
1618 move (helpoffset, 0);
1619 mutt_paddstr (COLS, helpstr);
1620 SETCOLOR (MT_COLOR_NORMAL);
1623 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1626 if ((SearchCompiled = Resize->SearchCompiled))
1629 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1630 SearchFlag = M_SEARCH;
1631 SearchBack = Resize->SearchBack;
1633 lines = Resize->line;
1634 redraw |= REDRAW_SIGWINCH;
1640 if (IsHeader (extra) && PagerIndexLines)
1644 /* only allocate the space if/when we need the index.
1645 Initialise the menu as per the main index */
1646 index = mutt_new_menu();
1647 index->menu = MENU_MAIN;
1648 index->make_entry = index_make_entry;
1649 index->color = index_color;
1650 index->max = Context->vcount;
1651 index->current = extra->hdr->virtual;
1654 SETCOLOR (MT_COLOR_NORMAL);
1655 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1657 index->pagelen = indexlen - 1;
1659 /* some fudge to work out where abouts the indicator should go */
1660 if (index->current - indicator < 0)
1662 else if (index->max - index->current < index->pagelen - indicator)
1663 index->top = index->max - index->pagelen;
1665 index->top = index->current - indicator;
1667 menu_redraw_index(index);
1670 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1674 if (redraw & REDRAW_SIGWINCH)
1678 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1679 has_types | SearchFlag, &QuoteList, &q_level, &force_redraw,
1681 if (!lineInfo[i].continuation && ++j == lines)
1689 if ((redraw & REDRAW_BODY) || topline != oldtopline)
1692 move (bodyoffset, SidebarWidth);
1693 curline = oldtopline = topline;
1697 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1)
1699 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1701 (flags & M_DISPLAYFLAGS) | hideQuoted | SearchFlag,
1702 &QuoteList, &q_level, &force_redraw, &SearchRE) > 0)
1705 move(lines + bodyoffset, SidebarWidth);
1707 last_offset = lineInfo[curline].offset;
1708 } while (force_redraw);
1710 SETCOLOR (MT_COLOR_TILDE);
1711 BKGDSET (MT_COLOR_TILDE);
1712 while (lines < bodylen)
1715 if (option (OPTTILDE))
1719 move(lines + bodyoffset, SidebarWidth);
1721 /* We are going to update the pager status bar, so it isn't
1722 * necessary to reset to normal color now. */
1724 redraw |= REDRAW_STATUS; /* need to update the % seen */
1727 if (redraw & REDRAW_STATUS)
1729 /* print out the pager status bar */
1730 SETCOLOR (MT_COLOR_STATUS);
1731 BKGDSET (MT_COLOR_STATUS);
1732 CLEARLINE_WIN(statusoffset);
1733 if (IsHeader (extra))
1735 _mutt_make_string (buffer,
1736 COLS-9-SidebarWidth < sizeof (buffer) ?
1737 COLS-9-SidebarWidth : sizeof (buffer),
1738 NONULL (PagerFmt), Context, extra->hdr, M_FORMAT_MAKEPRINT);
1740 else if (IsMsgAttach (extra))
1742 _mutt_make_string (buffer,
1743 COLS - 9 - SidebarWidth < sizeof (buffer) ?
1744 COLS - 9 - SidebarWidth : sizeof (buffer),
1745 NONULL (PagerFmt), Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1747 mutt_paddstr (COLS-10-SidebarWidth, IsHeader (extra) || IsMsgAttach (extra) ?
1750 if (last_pos < sb.st_size - 1)
1751 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1753 addstr (topline == 0 ? "all)" : "end)");
1754 BKGDSET (MT_COLOR_NORMAL);
1755 SETCOLOR (MT_COLOR_NORMAL);
1758 if ((redraw & REDRAW_INDEX) && index)
1760 /* redraw the pager_index indicator, because the
1761 * flags for this message might have changed. */
1762 menu_redraw_current (index);
1763 draw_sidebar(MENU_PAGER);
1764 /* print out the index status bar */
1765 menu_status_line (buffer, sizeof (buffer), index, NONULL(Status));
1766 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)),
1768 SETCOLOR (MT_COLOR_STATUS);
1769 mutt_paddstr (COLS-SidebarWidth, buffer);
1770 SETCOLOR (MT_COLOR_NORMAL);
1772 /* if we're not using the index, update every time */
1774 draw_sidebar(MENU_PAGER);
1778 move (statusoffset, COLS-1);
1780 ch = km_dokey (MENU_PAGER);
1782 mutt_clear_error ();
1790 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1793 mutt_resize_screen ();
1795 /* Store current position. */
1797 for (i = 0; i <= topline; i++)
1798 if (!lineInfo[i].continuation)
1801 if (flags & M_PAGER_RETWINCH)
1803 Resize = safe_malloc (sizeof (struct resize));
1805 Resize->line = lines;
1806 Resize->SearchCompiled = SearchCompiled;
1807 Resize->SearchBack = SearchBack;
1810 rc = OP_REFORMAT_WINCH;
1814 for (i = 0; i < maxLine; i++)
1816 lineInfo[i].offset = 0;
1817 lineInfo[i].type = -1;
1818 lineInfo[i].continuation = 0;
1819 lineInfo[i].chunks = 0;
1820 lineInfo[i].search_cnt = -1;
1821 lineInfo[i].quote = NULL;
1823 safe_realloc (&(lineInfo[i].syntax),
1824 sizeof (struct syntax_t));
1825 if (SearchCompiled && lineInfo[i].search)
1826 FREE (&(lineInfo[i].search));
1832 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1837 clearok(stdscr,TRUE);/*force complete redraw*/
1857 if (lineInfo[curline].offset < sb.st_size-1)
1859 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1861 else if (option (OPTPAGERSTOP))
1863 /* emulate "less -q" and don't go on to the next message. */
1864 mutt_error _("Bottom of message is shown.");
1868 /* end of the current message, so display the next message. */
1869 rc = OP_MAIN_NEXT_UNDELETED;
1877 topline = upNLines (bodylen-PagerContext, lineInfo, topline, hideQuoted);
1880 mutt_error _("Top of message is shown.");
1884 if (lineInfo[curline].offset < sb.st_size-1)
1889 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1895 mutt_error _("Bottom of message is shown.");
1900 topline = upNLines (1, lineInfo, topline, hideQuoted);
1902 mutt_error _("Top of message is shown.");
1909 mutt_error _("Top of message is shown.");
1914 topline = upNLines (bodylen/2, lineInfo, topline, hideQuoted);
1916 mutt_error _("Top of message is shown.");
1920 if (lineInfo[curline].offset < sb.st_size-1)
1922 topline = upNLines (bodylen/2, lineInfo, curline, hideQuoted);
1924 else if (option (OPTPAGERSTOP))
1926 /* emulate "less -q" and don't go on to the next message. */
1927 mutt_error _("Bottom of message is shown.");
1931 /* end of the current message, so display the next message. */
1932 rc = OP_MAIN_NEXT_UNDELETED;
1937 case OP_SEARCH_NEXT:
1938 case OP_SEARCH_OPPOSITE:
1942 if ((!SearchBack && ch==OP_SEARCH_NEXT) ||
1943 (SearchBack &&ch==OP_SEARCH_OPPOSITE))
1945 /* searching forward */
1946 for (i = topline + 1; i < lastLine; i++)
1948 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1949 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1956 mutt_error _("Not found.");
1960 /* searching backward */
1961 for (i = topline - 1; i >= 0; i--)
1963 if ((!hideQuoted || (has_types &&
1964 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1965 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1972 mutt_error _("Not found.");
1975 if (lineInfo[topline].search_cnt > 0)
1976 SearchFlag = M_SEARCH;
1980 /* no previous search pattern, so fall through to search */
1983 case OP_SEARCH_REVERSE:
1984 strfcpy (buffer, searchbuf, sizeof (buffer));
1985 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1986 _("Search: ")), buffer, sizeof (buffer),
1990 if (!strcmp (buffer, searchbuf))
1994 /* do an implicit search-next */
1995 if (ch == OP_SEARCH)
1996 ch = OP_SEARCH_NEXT;
1998 ch = OP_SEARCH_OPPOSITE;
2007 strfcpy (searchbuf, buffer, sizeof (searchbuf));
2009 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
2010 if (ch == OP_SEARCH)
2012 else if (ch == OP_SEARCH_REVERSE)
2017 regfree (&SearchRE);
2018 for (i = 0; i < lastLine; i++)
2020 if (lineInfo[i].search)
2021 FREE (&(lineInfo[i].search));
2022 lineInfo[i].search_cnt = -1;
2026 if ((err = REGCOMP (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf))) != 0)
2028 regerror (err, &SearchRE, buffer, sizeof (buffer));
2029 mutt_error ("%s", buffer);
2030 regfree (&SearchRE);
2031 for (i = 0; i < maxLine ; i++)
2034 if (lineInfo[i].search)
2035 FREE (&(lineInfo[i].search));
2036 lineInfo[i].search_cnt = -1;
2044 /* update the search pointers */
2046 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2047 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
2048 &QuoteList, &q_level,
2049 &force_redraw, &SearchRE) == 0)
2054 /* searching forward */
2055 for (i = topline; i < lastLine; i++)
2057 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2058 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2062 if (i < lastLine) topline = i;
2066 /* searching backward */
2067 for (i = topline; i >= 0; i--)
2069 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2070 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2074 if (i >= 0) topline = i;
2077 if (lineInfo[topline].search_cnt == 0)
2080 mutt_error _("Not found.");
2083 SearchFlag = M_SEARCH;
2085 redraw = REDRAW_BODY;
2088 case OP_SEARCH_TOGGLE:
2091 SearchFlag ^= M_SEARCH;
2092 redraw = REDRAW_BODY;
2097 /* don't let the user enter the help-menu from the help screen! */
2101 mutt_help (MENU_PAGER);
2102 redraw = REDRAW_FULL;
2106 mutt_error _("Help is currently being shown.");
2109 case OP_PAGER_HIDE_QUOTED:
2112 hideQuoted ^= M_HIDE;
2113 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
2114 topline = upNLines (1, lineInfo, topline, hideQuoted);
2116 redraw = REDRAW_BODY;
2120 case OP_PAGER_SKIP_QUOTED:
2124 int new_topline = topline;
2126 while ((new_topline < lastLine ||
2127 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2128 new_topline, &lastLine, &maxLine, M_TYPES,
2129 &QuoteList, &q_level, &force_redraw, &SearchRE))))
2130 && lineInfo[new_topline].type != MT_COLOR_QUOTED)
2135 mutt_error _("No more quoted text.");
2139 while ((new_topline < lastLine ||
2140 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2141 new_topline, &lastLine, &maxLine, M_TYPES,
2142 &QuoteList, &q_level, &force_redraw, &SearchRE))))
2143 && lineInfo[new_topline].type == MT_COLOR_QUOTED)
2148 mutt_error _("No more unquoted text after quoted text.");
2151 topline = new_topline;
2155 case OP_PAGER_BOTTOM: /* move to the end of the file */
2156 if (lineInfo[curline].offset < sb.st_size - 1)
2159 /* make sure the types are defined to the end of file */
2160 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2161 &maxLine, has_types,
2162 &QuoteList, &q_level, &force_redraw,
2165 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2168 mutt_error _("Bottom of message is shown.");
2172 clearok (stdscr, TRUE);
2173 redraw = REDRAW_FULL;
2177 km_error_key (MENU_PAGER);
2180 /* --------------------------------------------------------------------
2181 * The following are operations on the current message rather than
2182 * adjusting the view of the message.
2185 case OP_BOUNCE_MESSAGE:
2186 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra))
2188 if (IsMsgAttach (extra))
2189 mutt_attach_bounce (extra->fp, extra->hdr,
2190 extra->idx, extra->idxlen,
2193 ci_bounce_message (extra->hdr, &redraw);
2197 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra))
2199 if (IsMsgAttach (extra))
2200 mutt_attach_resend (extra->fp, extra->hdr,
2201 extra->idx, extra->idxlen,
2204 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2205 redraw = REDRAW_FULL;
2208 case OP_CREATE_ALIAS:
2209 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2210 if (IsMsgAttach (extra))
2211 mutt_create_alias (extra->bdy->hdr->env, NULL);
2213 mutt_create_alias (extra->hdr->env, NULL);
2214 MAYBE_REDRAW (redraw);
2217 case OP_PURGE_MESSAGE:
2219 CHECK_MODE(IsHeader (extra));
2223 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2226 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2227 mutt_set_flag (Context, extra->hdr, M_PURGED,
2228 ch != OP_PURGE_MESSAGE ? 0 : 1);
2229 if (option (OPTDELETEUNTAG))
2230 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2231 redraw = REDRAW_STATUS | REDRAW_INDEX;
2232 if (option (OPTRESOLVE))
2235 rc = OP_MAIN_NEXT_UNDELETED;
2239 case OP_DELETE_THREAD:
2240 case OP_DELETE_SUBTHREAD:
2241 CHECK_MODE(IsHeader (extra));
2245 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2248 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2249 ch == OP_DELETE_THREAD ? 0 : 1);
2253 if (option (OPTDELETEUNTAG))
2254 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2255 ch == OP_DELETE_THREAD ? 0 : 1);
2256 if (option (OPTRESOLVE))
2258 rc = OP_MAIN_NEXT_UNDELETED;
2262 if (!option (OPTRESOLVE) && PagerIndexLines)
2263 redraw = REDRAW_FULL;
2265 redraw = REDRAW_STATUS | REDRAW_INDEX;
2269 case OP_DISPLAY_ADDRESS:
2270 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2271 if (IsMsgAttach (extra))
2272 mutt_display_address (extra->bdy->hdr->env);
2274 mutt_display_address (extra->hdr->env);
2277 case OP_ENTER_COMMAND:
2278 old_smart_wrap = option (OPTWRAP);
2279 old_markers = option (OPTMARKERS);
2280 old_PagerIndexLines = PagerIndexLines;
2282 CurrentMenu = MENU_PAGER;
2283 mutt_enter_command ();
2285 if (option (OPTNEEDRESORT))
2287 unset_option (OPTNEEDRESORT);
2288 CHECK_MODE(IsHeader (extra));
2289 set_option (OPTNEEDRESORT);
2292 if (old_PagerIndexLines != PagerIndexLines)
2295 mutt_menuDestroy (&index);
2299 if (option (OPTWRAP) != old_smart_wrap ||
2300 option (OPTMARKERS) != old_markers)
2302 if (flags & M_PAGER_RETWINCH)
2305 rc = OP_REFORMAT_WINCH;
2309 /* count the real lines above */
2311 for (i = 0; i <= topline; i++)
2313 if (!lineInfo[i].continuation)
2317 /* we need to restart the whole thing */
2318 for (i = 0; i < maxLine; i++)
2320 lineInfo[i].offset = 0;
2321 lineInfo[i].type = -1;
2322 lineInfo[i].continuation = 0;
2323 lineInfo[i].chunks = 0;
2324 lineInfo[i].search_cnt = -1;
2325 lineInfo[i].quote = NULL;
2327 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2328 if (SearchCompiled && lineInfo[i].search)
2329 FREE (&(lineInfo[i].search));
2334 regfree (&SearchRE);
2339 /* try to keep the old position */
2342 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2343 &lastLine, &maxLine,
2344 (has_types ? M_TYPES : 0),
2345 &QuoteList, &q_level, &force_redraw,
2348 if (! lineInfo[topline].continuation)
2357 if (option (OPTFORCEREDRAWPAGER))
2358 redraw = REDRAW_FULL;
2359 unset_option (OPTFORCEREDRAWINDEX);
2360 unset_option (OPTFORCEREDRAWPAGER);
2363 case OP_FLAG_MESSAGE:
2364 CHECK_MODE(IsHeader (extra));
2368 if (Context->magic == M_POP)
2371 mutt_error _("Can't change 'important' flag on POP server.");
2377 CHECK_IMAP_ACL(IMAP_ACL_WRITE);
2381 if (Context->magic == M_NNTP)
2384 mutt_error _("Can't change 'important' flag on NNTP server.");
2389 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2390 redraw = REDRAW_STATUS | REDRAW_INDEX;
2391 if (option (OPTRESOLVE))
2394 rc = OP_MAIN_NEXT_UNDELETED;
2399 CHECK_MODE(IsHeader (extra) || IsAttach (extra));
2400 if (IsAttach (extra))
2401 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2403 mutt_pipe_message (extra->hdr);
2404 MAYBE_REDRAW (redraw);
2408 CHECK_MODE(IsHeader (extra) || IsAttach (extra));
2409 if (IsAttach (extra))
2410 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2412 mutt_print_message (extra->hdr);
2416 CHECK_MODE(IsHeader (extra) && !IsAttach (extra));
2418 ci_send_message (0, NULL, NULL, extra->ctx, extra->hdr);
2419 redraw = REDRAW_FULL;
2424 CHECK_MODE(IsHeader (extra) && !IsAttach (extra));
2426 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2427 !((NNTP_DATA *)extra->ctx->data)->allowed &&
2428 query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
2430 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2431 redraw = REDRAW_FULL;
2434 case OP_FORWARD_TO_GROUP:
2435 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2437 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2438 !((NNTP_DATA *)extra->ctx->data)->allowed &&
2439 query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
2441 if (IsMsgAttach (extra))
2442 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2443 extra->idxlen, extra->bdy, SENDNEWS);
2445 ci_send_message (SENDNEWS|SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2446 redraw = REDRAW_FULL;
2450 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2453 if (IsMsgAttach (extra))
2454 followup_to = extra->bdy->hdr->env->followup_to;
2456 followup_to = extra->hdr->env->followup_to;
2458 if (!followup_to || mutt_strcasecmp (followup_to, "poster") ||
2459 query_quadoption (OPT_FOLLOWUPTOPOSTER,_("Reply by mail as poster prefers?")) != M_YES)
2461 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2462 !((NNTP_DATA *)extra->ctx->data)->allowed &&
2463 query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
2465 if (IsMsgAttach (extra))
2466 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2467 extra->idxlen, extra->bdy, SENDNEWS|SENDREPLY);
2469 ci_send_message (SENDNEWS|SENDREPLY, NULL, NULL,
2470 extra->ctx, extra->hdr);
2471 redraw = REDRAW_FULL;
2477 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2479 if (IsMsgAttach (extra))
2480 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2481 extra->idxlen, extra->bdy,
2484 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2485 redraw = REDRAW_FULL;
2488 case OP_RECALL_MESSAGE:
2489 CHECK_MODE(IsHeader (extra));
2491 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2492 redraw = REDRAW_FULL;
2495 case OP_GROUP_REPLY:
2496 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2498 if (IsMsgAttach (extra))
2499 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2500 extra->idxlen, extra->bdy, SENDREPLY|SENDGROUPREPLY);
2502 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx, extra->hdr);
2503 redraw = REDRAW_FULL;
2507 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2509 if (IsMsgAttach (extra))
2510 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2511 extra->idxlen, extra->bdy, SENDREPLY|SENDLISTREPLY);
2513 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx, extra->hdr);
2514 redraw = REDRAW_FULL;
2517 case OP_FORWARD_MESSAGE:
2518 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2520 if (IsMsgAttach (extra))
2521 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2522 extra->idxlen, extra->bdy, 0);
2524 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2525 redraw = REDRAW_FULL;
2528 case OP_DECRYPT_SAVE:
2536 if (IsAttach (extra))
2538 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr, NULL);
2542 case OP_COPY_MESSAGE:
2543 case OP_DECODE_SAVE:
2544 case OP_DECODE_COPY:
2545 case OP_DECRYPT_COPY:
2546 if (!WithCrypto && ch == OP_DECRYPT_COPY)
2551 CHECK_MODE(IsHeader (extra));
2552 if (mutt_save_message (extra->hdr,
2553 (ch == OP_DECRYPT_SAVE) ||
2554 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2555 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2556 (ch == OP_DECRYPT_SAVE) || (ch == OP_DECRYPT_COPY) ||
2558 &redraw) == 0 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2559 || ch == OP_DECRYPT_SAVE
2562 if (option (OPTRESOLVE))
2565 rc = OP_MAIN_NEXT_UNDELETED;
2568 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2570 MAYBE_REDRAW (redraw);
2573 case OP_SHELL_ESCAPE:
2574 mutt_shell_escape ();
2575 MAYBE_REDRAW (redraw);
2579 CHECK_MODE(IsHeader (extra));
2580 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2582 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2583 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2584 ? NULL : Context->last_tag);
2586 redraw = REDRAW_STATUS | REDRAW_INDEX;
2587 if (option (OPTRESOLVE))
2595 CHECK_MODE(IsHeader (extra));
2599 CHECK_IMAP_ACL(IMAP_ACL_SEEN);
2602 if (extra->hdr->read || extra->hdr->old)
2603 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2605 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2607 Context->msgnotreadyet = -1;
2608 redraw = REDRAW_STATUS | REDRAW_INDEX;
2609 if (option (OPTRESOLVE))
2612 rc = OP_MAIN_NEXT_UNDELETED;
2617 CHECK_MODE(IsHeader (extra));
2621 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2624 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2625 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2626 redraw = REDRAW_STATUS | REDRAW_INDEX;
2627 if (option (OPTRESOLVE))
2634 case OP_UNDELETE_THREAD:
2635 case OP_UNDELETE_SUBTHREAD:
2636 CHECK_MODE(IsHeader (extra));
2640 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2643 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2644 ch == OP_UNDELETE_THREAD ? 0 : 1)
2645 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2646 ch == OP_UNDELETE_THREAD ? 0 : 1);
2650 if (option (OPTRESOLVE))
2652 rc = (ch == OP_DELETE_THREAD) ?
2653 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2657 if (!option (OPTRESOLVE) && PagerIndexLines)
2658 redraw = REDRAW_FULL;
2660 redraw = REDRAW_STATUS | REDRAW_INDEX;
2672 case OP_VIEW_ATTACHMENTS:
2673 if (flags & M_PAGER_ATTACHMENT)
2676 rc = OP_ATTACH_COLLAPSE;
2679 CHECK_MODE(IsHeader (extra));
2680 mutt_view_attachments (extra->hdr);
2681 if (extra->hdr->attach_del)
2682 Context->changed = 1;
2683 redraw = REDRAW_FULL;
2688 if (!(WithCrypto & APPLICATION_PGP))
2693 CHECK_MODE(IsHeader(extra));
2695 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2696 redraw = REDRAW_FULL;
2700 case OP_FORGET_PASSPHRASE:
2701 crypt_forget_passphrase ();
2704 case OP_EXTRACT_KEYS:
2710 CHECK_MODE(IsHeader(extra));
2711 crypt_extract_keys_from_messages(extra->hdr);
2712 redraw = REDRAW_FULL;
2715 case OP_SIDEBAR_SCROLL_UP:
2716 case OP_SIDEBAR_SCROLL_DOWN:
2717 case OP_SIDEBAR_NEXT:
2718 case OP_SIDEBAR_PREV:
2719 scroll_sidebar(ch, MENU_PAGER);
2728 if (IsHeader (extra))
2729 Context->msgnotreadyet = -1;
2731 cleanup_quote (&QuoteList);
2733 for (i = 0; i < maxLine ; i++)
2735 FREE (&(lineInfo[i].syntax));
2736 if (SearchCompiled && lineInfo[i].search)
2737 FREE (&(lineInfo[i].search));
2741 regfree (&SearchRE);
2746 mutt_menuDestroy(&index);
2747 return (rc != -1 ? rc : 0);