2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
5 * Parts were written/modified by:
6 * Nico Golde <nico@ngolde.de>
7 * Andreas Krennmair <ak@synflood.at>
9 * This file is part of mutt-ng, see http://www.muttng.org/.
10 * It's licensed under the GNU General Public License,
11 * please see the file GPL in the top level source directory.
19 #include "mutt_curses.h"
21 #include "mutt_menu.h"
32 #include "imap_private.h"
35 #include "mutt_crypt.h"
49 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
51 #define IsAttach(x) (x && (x)->bdy)
52 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
53 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
54 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
55 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
57 static const char *Not_available_in_this_menu =
58 N_("Not available in this menu.");
59 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
60 static const char *Function_not_permitted_in_attach_message_mode =
61 N_("Function not permitted in attach-message mode.");
63 #define CHECK_MODE(x) if (!(x)) \
66 mutt_error _(Not_available_in_this_menu); \
70 #define CHECK_READONLY if (Context->readonly) \
73 mutt_error _(Mailbox_is_read_only); \
77 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
80 mutt_error _(Function_not_permitted_in_attach_message_mode); \
89 struct q_class_t *next, *prev;
90 struct q_class_t *down, *up;
105 struct syntax_t *syntax;
106 struct syntax_t *search;
107 struct q_class_t *quote;
110 #define ANSI_OFF (1<<0)
111 #define ANSI_BLINK (1<<1)
112 #define ANSI_BOLD (1<<2)
113 #define ANSI_UNDERLINE (1<<3)
114 #define ANSI_REVERSE (1<<4)
115 #define ANSI_COLOR (1<<5)
117 typedef struct _ansi_attr {
124 static short InHelp = 0;
126 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
127 static struct resize {
134 #define NumSigLines 4
136 static int check_sig (const char *s, struct line_t *info, int n)
140 while (n > 0 && count <= NumSigLines) {
141 if (info[n].type != MT_COLOR_SIGNATURE)
150 if (count > NumSigLines) {
151 /* check for a blank line */
165 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
166 int special, ansi_attr * a)
168 int def_color; /* color without syntax hilight */
169 int color; /* final color */
170 static int last_color; /* last color set */
171 int search = 0, i, m;
174 last_color = -1; /* force attrset() */
176 if (lineInfo[n].continuation) {
177 if (!cnt && option (OPTMARKERS)) {
178 SETCOLOR (MT_COLOR_MARKERS);
180 last_color = ColorDefs[MT_COLOR_MARKERS];
182 m = (lineInfo[n].syntax)[0].first;
183 cnt += (lineInfo[n].syntax)[0].last;
187 if (!(flags & M_SHOWCOLOR))
188 def_color = ColorDefs[MT_COLOR_NORMAL];
189 else if (lineInfo[m].type == MT_COLOR_HEADER)
190 def_color = (lineInfo[m].syntax)[0].color;
192 def_color = ColorDefs[lineInfo[m].type];
194 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
195 struct q_class_t *class = lineInfo[m].quote;
198 def_color = class->color;
200 while (class && class->length > cnt) {
201 def_color = class->color;
208 if (flags & M_SHOWCOLOR) {
209 for (i = 0; i < lineInfo[m].chunks; i++) {
210 /* we assume the chunks are sorted */
211 if (cnt > (lineInfo[m].syntax)[i].last)
213 if (cnt < (lineInfo[m].syntax)[i].first)
215 if (cnt != (lineInfo[m].syntax)[i].last) {
216 color = (lineInfo[m].syntax)[i].color;
219 /* don't break here, as cnt might be
220 * in the next chunk as well */
224 if (flags & M_SEARCH) {
225 for (i = 0; i < lineInfo[m].search_cnt; i++) {
226 if (cnt > (lineInfo[m].search)[i].last)
228 if (cnt < (lineInfo[m].search)[i].first)
230 if (cnt != (lineInfo[m].search)[i].last) {
231 color = ColorDefs[MT_COLOR_SEARCH];
238 /* handle "special" bold & underlined characters */
239 if (special || a->attr) {
241 if ((a->attr & ANSI_COLOR)) {
243 a->pair = mutt_alloc_color (a->fg, a->bg);
245 if (a->attr & ANSI_BOLD)
250 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
251 if (ColorDefs[MT_COLOR_BOLD] && !search)
252 color = ColorDefs[MT_COLOR_BOLD];
256 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
257 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
258 color = ColorDefs[MT_COLOR_UNDERLINE];
260 color ^= A_UNDERLINE;
262 else if (a->attr & ANSI_REVERSE) {
265 else if (a->attr & ANSI_BLINK) {
268 else if (a->attr & ANSI_OFF) {
273 if (color != last_color) {
279 static void append_line (struct line_t *lineInfo, int n, int cnt)
283 lineInfo[n + 1].type = lineInfo[n].type;
284 (lineInfo[n + 1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
285 lineInfo[n + 1].continuation = 1;
287 /* find the real start of the line */
288 for (m = n; m >= 0; m--)
289 if (lineInfo[m].continuation == 0)
292 (lineInfo[n + 1].syntax)[0].first = m;
293 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
294 cnt + (lineInfo[n].syntax)[0].last : cnt;
297 static void new_class_color (struct q_class_t *class, int *q_level)
299 class->index = (*q_level)++;
300 class->color = ColorQuote[class->index % ColorQuoteUsed];
304 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
305 int index, int *q_level)
307 struct q_class_t *q_list;
310 new_class->index = -1;
313 if (q_list->index >= index) {
315 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
318 q_list = q_list->down;
319 else if (q_list->next)
320 q_list = q_list->next;
322 while (!q_list->next) {
328 q_list = q_list->next;
332 new_class->index = index;
333 new_class->color = ColorQuote[index % ColorQuoteUsed];
337 static void cleanup_quote (struct q_class_t **QuoteList)
339 struct q_class_t *ptr;
342 if ((*QuoteList)->down)
343 cleanup_quote (&((*QuoteList)->down));
344 ptr = (*QuoteList)->next;
345 if ((*QuoteList)->prefix)
346 FREE (&(*QuoteList)->prefix);
354 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
355 const char *qptr, int length,
356 int *force_redraw, int *q_level)
358 struct q_class_t *q_list = *QuoteList;
359 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
361 int offset, tail_lng;
364 if (ColorQuoteUsed <= 1) {
365 /* not much point in classifying quotes... */
367 if (*QuoteList == NULL) {
368 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
369 class->color = ColorQuote[0];
375 /* Did I mention how much I like emulating Lisp in C? */
377 /* classify quoting prefix */
379 if (length <= q_list->length) {
380 /* case 1: check the top level nodes */
382 if (safe_strncmp (qptr, q_list->prefix, length) == 0) {
383 if (length == q_list->length)
384 return q_list; /* same prefix: return the current class */
386 /* found shorter prefix */
388 /* add a node above q_list */
390 (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
391 tmp->prefix = (char *) safe_calloc (1, length + 1);
392 strncpy (tmp->prefix, qptr, length);
393 tmp->length = length;
395 /* replace q_list by tmp in the top level list */
397 tmp->next = q_list->next;
398 q_list->next->prev = tmp;
401 tmp->prev = q_list->prev;
402 q_list->prev->next = tmp;
405 /* make q_list a child of tmp */
409 /* q_list has no siblings for now */
413 /* update the root if necessary */
414 if (q_list == *QuoteList)
417 index = q_list->index;
419 /* tmp should be the return class too */
422 /* next class to test; if tmp is a shorter prefix for another
423 * node, that node can only be in the top level list, so don't
424 * go down after this point
429 /* found another branch for which tmp is a shorter prefix */
431 /* save the next sibling for later */
434 /* unlink q_list from the top level list */
436 q_list->next->prev = q_list->prev;
438 q_list->prev->next = q_list->next;
440 /* at this point, we have a tmp->down; link q_list to it */
442 /* sibling order is important here, q_list should be linked last */
450 index = q_list->index;
452 /* next class to test; as above, we shouldn't go down */
456 /* we found a shorter prefix, so certain quotes have changed classes */
461 /* shorter, but not a substring of the current class: try next */
462 q_list = q_list->next;
467 /* case 2: try subclassing the current top level node */
469 /* tmp != NULL means we already found a shorter prefix at case 1 */
471 && safe_strncmp (qptr, q_list->prefix, q_list->length) == 0) {
472 /* ok, it's a subclass somewhere on this branch */
475 offset = q_list->length;
477 q_list = q_list->down;
478 tail_lng = length - offset;
479 tail_qptr = (char *) qptr + offset;
482 if (length <= q_list->length) {
483 if (safe_strncmp (tail_qptr, (q_list->prefix) + offset, tail_lng)
485 /* same prefix: return the current class */
486 if (length == q_list->length)
489 /* found shorter common prefix */
491 /* add a node above q_list */
492 tmp = (struct q_class_t *) safe_calloc (1,
495 tmp->prefix = (char *) safe_calloc (1, length + 1);
496 strncpy (tmp->prefix, qptr, length);
497 tmp->length = length;
499 /* replace q_list by tmp */
501 tmp->next = q_list->next;
502 q_list->next->prev = tmp;
505 tmp->prev = q_list->prev;
506 q_list->prev->next = tmp;
509 /* make q_list a child of tmp */
511 tmp->up = q_list->up;
513 if (tmp->up->down == q_list)
516 /* q_list has no siblings */
520 index = q_list->index;
522 /* tmp should be the return class too */
525 /* next class to test */
529 /* found another branch for which tmp is a shorter prefix */
531 /* save the next sibling for later */
534 /* unlink q_list from the top level list */
536 q_list->next->prev = q_list->prev;
538 q_list->prev->next = q_list->next;
540 /* at this point, we have a tmp->down; link q_list to it */
549 index = q_list->index;
551 /* next class to test */
555 /* we found a shorter prefix, so we need a redraw */
560 q_list = q_list->next;
565 /* longer than the current prefix: try subclassing it */
567 && safe_strncmp (tail_qptr, (q_list->prefix) + offset,
568 q_list->length - offset) == 0) {
569 /* still a subclass: go down one level */
571 offset = q_list->length;
573 q_list = q_list->down;
574 tail_lng = length - offset;
575 tail_qptr = (char *) qptr + offset;
580 /* nope, try the next prefix */
581 q_list = q_list->next;
587 /* still not found so far: add it as a sibling to the current node */
590 (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
591 tmp->prefix = (char *) safe_calloc (1, length + 1);
592 strncpy (tmp->prefix, qptr, length);
593 tmp->length = length;
596 tmp->next = ptr->down;
597 ptr->down->prev = tmp;
602 new_class_color (tmp, q_level);
608 shift_class_colors (*QuoteList, tmp, index, q_level);
614 /* nope, try the next prefix */
615 q_list = q_list->next;
622 /* not found so far: add it as a top level class */
623 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
624 class->prefix = (char *) safe_calloc (1, length + 1);
625 strncpy (class->prefix, qptr, length);
626 class->length = length;
627 new_class_color (class, q_level);
630 class->next = *QuoteList;
631 (*QuoteList)->prev = class;
637 shift_class_colors (*QuoteList, tmp, index, q_level);
642 static int check_attachment_marker (char *);
645 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
646 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
649 COLOR_LINE *color_line;
650 regmatch_t pmatch[1], smatch[1];
651 int found, offset, null_rx, i;
653 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
655 lineInfo[n].type = MT_COLOR_NORMAL;
656 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
657 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
658 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
661 lineInfo[n].type = MT_COLOR_HDEFAULT;
662 color_line = ColorHdrList;
664 if (REGEXEC (&color_line->rx, buf) == 0) {
665 lineInfo[n].type = MT_COLOR_HEADER;
666 lineInfo[n].syntax[0].color = color_line->pair;
669 color_line = color_line->next;
673 else if (safe_strncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
674 lineInfo[n].type = MT_COLOR_NORMAL;
676 else if (safe_strncmp ("[-- ", buf, 4) == 0)
677 lineInfo[n].type = MT_COLOR_ATTACHMENT;
679 else if (check_attachment_marker ((char *) raw) == 0)
680 lineInfo[n].type = MT_COLOR_ATTACHMENT;
682 else if (safe_strcmp ("-- \n", buf) == 0
683 || safe_strcmp ("-- \r\n", buf) == 0) {
686 lineInfo[n].type = MT_COLOR_SIGNATURE;
687 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
688 (lineInfo[i].type == MT_COLOR_NORMAL ||
689 lineInfo[i].type == MT_COLOR_QUOTED ||
690 lineInfo[i].type == MT_COLOR_HEADER)) {
692 if (lineInfo[i].chunks) {
693 lineInfo[i].chunks = 0;
694 safe_realloc (&(lineInfo[n].syntax), sizeof (struct syntax_t));
696 lineInfo[i++].type = MT_COLOR_SIGNATURE;
699 else if (check_sig (buf, lineInfo, n - 1) == 0)
700 lineInfo[n].type = MT_COLOR_SIGNATURE;
701 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
702 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
703 if (smatch[0].rm_so > 0) {
706 /* hack to avoid making an extra copy of buf */
707 c = buf[smatch[0].rm_so];
708 buf[smatch[0].rm_so] = 0;
710 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
711 if (q_classify && lineInfo[n].quote == NULL)
712 lineInfo[n].quote = classify_quote (QuoteList,
713 buf + pmatch[0].rm_so,
715 pmatch[0].rm_so, force_redraw,
717 lineInfo[n].type = MT_COLOR_QUOTED;
720 lineInfo[n].type = MT_COLOR_NORMAL;
722 buf[smatch[0].rm_so] = c;
725 lineInfo[n].type = MT_COLOR_NORMAL;
728 if (q_classify && lineInfo[n].quote == NULL)
729 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
730 pmatch[0].rm_eo - pmatch[0].rm_so,
731 force_redraw, q_level);
732 lineInfo[n].type = MT_COLOR_QUOTED;
736 lineInfo[n].type = MT_COLOR_NORMAL;
739 if (lineInfo[n].type == MT_COLOR_NORMAL ||
740 lineInfo[n].type == MT_COLOR_QUOTED) {
744 lineInfo[n].chunks = 0;
751 color_line = ColorBodyList;
753 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
754 (offset ? REG_NOTBOL : 0)) == 0) {
755 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
757 if (++(lineInfo[n].chunks) > 1)
758 safe_realloc (&(lineInfo[n].syntax),
759 (lineInfo[n].chunks) *
760 sizeof (struct syntax_t));
762 i = lineInfo[n].chunks - 1;
763 pmatch[0].rm_so += offset;
764 pmatch[0].rm_eo += offset;
766 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
767 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
768 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
769 (lineInfo[n].syntax)[i].color = color_line->pair;
770 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
771 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
777 null_rx = 1; /* empty regexp; don't add it, but keep looking */
779 color_line = color_line->next;
783 offset++; /* avoid degenerate cases */
785 offset = (lineInfo[n].syntax)[i].last;
786 } while (found || null_rx);
790 static int is_ansi (unsigned char *buf)
792 while (*buf && (isdigit (*buf) || *buf == ';'))
794 return (*buf == 'm');
797 static int check_attachment_marker (char *p)
799 char *q = AttachmentMarker;
801 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
802 return (int) (*p - *q);
805 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
809 while (isdigit (buf[x]) || buf[x] == ';')
812 /* Character Attributes */
813 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
817 mutt_free_color (a->fg, a->bg);
823 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
824 a->attr |= ANSI_BOLD;
827 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
828 a->attr |= ANSI_UNDERLINE;
831 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
832 a->attr |= ANSI_BLINK;
835 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
836 a->attr |= ANSI_REVERSE;
839 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
842 mutt_free_color (a->fg, a->bg);
848 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
851 mutt_free_color (a->fg, a->bg);
854 a->attr |= ANSI_COLOR;
855 a->fg = buf[pos + 1] - '0';
858 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
861 mutt_free_color (a->fg, a->bg);
864 a->attr |= ANSI_COLOR;
865 a->bg = buf[pos + 1] - '0';
869 while (pos < x && buf[pos] != ';')
880 fill_buffer (FILE * f, long *last_pos, long offset, unsigned char *buf,
881 unsigned char *fmt, size_t blen, int *buf_ready)
884 static int b_read = 0;
886 if (*buf_ready == 0) {
888 if (offset != *last_pos)
889 fseek (f, offset, 0);
890 if (fgets ((char *) buf, blen - 1, f) == NULL) {
894 *last_pos = ftell (f);
895 b_read = (int) (*last_pos - offset);
898 /* copy "buf" to "fmt", but without bold and underline controls */
901 if (*p == '\010' && (p > buf)) {
902 if (*(p + 1) == '_') /* underline */
904 else if (*(p + 1)) { /* bold or overstrike */
905 *(fmt - 1) = *(p + 1);
911 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
912 while (*p++ != 'm') /* skip ANSI sequence */
915 else if (*p == '\033' && *(p + 1) == ']'
916 && check_attachment_marker ((char *) p) == 0) {
917 dprint (2, (debugfile, "fill_buffer: Seen attachment marker.\n"));
918 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
935 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
936 int flags, ansi_attr * pa, int cnt,
937 int *pspace, int *pvch, int *pcol, int *pspecial)
939 int space = -1; /* index of the last space or TAB */
940 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
941 int ch, vch, k, last_special = -1, special = 0, t;
945 int wrap_cols = COLS;
947 if (!(flags & (M_SHOWFLAT)))
948 wrap_cols -= WrapMargin;
949 wrap_cols -= SidebarWidth;
954 /* FIXME: this should come from lineInfo */
955 memset (&mbstate, 0, sizeof (mbstate));
957 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
958 /* Handle ANSI sequences */
959 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
960 is_ansi (buf + ch + 2))
961 ch = grok_ansi (buf, ch + 2, pa) + 1;
963 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
964 check_attachment_marker ((char *) buf + ch) == 0) {
965 while (buf[ch++] != '\a')
970 /* is anything left to do? */
974 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
975 if (k == -2 || k == -1) {
976 dprint (1, (debugfile, "%s:%d: mbrtowc returned %d; errno = %d.\n",
977 __FILE__, __LINE__, k, errno));
978 if (col + 4 > wrap_cols)
982 printw ("\\%03o", buf[ch]);
989 /* Handle backspace */
996 while ((wc1 = 0, mbstate1 = mbstate,
998 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
999 &mbstate1), k1 - k > 0 && wc1 == '\b')
1001 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1002 &mbstate1), k2 > 0 && IsWPrint (wc1))) {
1004 special |= (wc == '_' && special & A_UNDERLINE)
1005 ? A_UNDERLINE : A_BOLD;
1007 else if (wc == '_' || wc1 == '_') {
1008 special |= A_UNDERLINE;
1009 wc = (wc1 == '_') ? wc : wc1;
1012 /* special = 0; / * overstrike: nothing to do! */
1022 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1023 special || last_special || pa->attr)) {
1024 resolve_color (*lineInfo, n, vch, flags, special, pa);
1025 last_special = special;
1028 if (IsWPrint (wc)) {
1032 if (col + t > wrap_cols)
1038 else if (wc == '\n')
1040 else if (wc == '\t') {
1046 for (; col < t; col++)
1051 else if (wc < 0x20 || wc == 0x7f) {
1052 if (col + 2 > wrap_cols)
1056 printw ("^%c", ('@' + wc) & 0x7f);
1058 else if (wc < 0x100) {
1059 if (col + 4 > wrap_cols)
1063 printw ("\\%03o", wc);
1066 if (col + 1 > wrap_cols)
1070 addch (replacement_char ());
1076 *pspecial = special;
1082 * flags M_SHOWFLAT, show characters (used for displaying help)
1083 * M_SHOWCOLOR, show characters in color
1084 * otherwise don't show characters
1085 * M_HIDE, don't show quoted text
1086 * M_SEARCH, resolve search patterns
1087 * M_TYPES, compute line's type
1088 * M_PAGER_NSKIP, keeps leading whitespace
1089 * M_PAGER_MARKER, eventually show markers
1092 * -1 EOF was reached
1093 * 0 normal exit, line was not displayed
1094 * >0 normal exit, line was displayed
1098 display_line (FILE * f, long *last_pos, struct line_t **lineInfo, int n,
1099 int *last, int *max, int flags, struct q_class_t **QuoteList,
1100 int *q_level, int *force_redraw, regex_t * SearchRE)
1102 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1103 unsigned char *buf_ptr = buf;
1104 int ch, vch, col, cnt, b_read;
1105 int buf_ready = 0, change_last = 0;
1110 ansi_attr a = { 0, 0, 0, -1 };
1111 regmatch_t pmatch[1];
1118 if (*last == *max) {
1119 safe_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1120 for (ch = *last; ch < *max; ch++) {
1121 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1122 (*lineInfo)[ch].type = -1;
1123 (*lineInfo)[ch].search_cnt = -1;
1124 (*lineInfo)[ch].syntax = safe_malloc (sizeof (struct syntax_t));
1125 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1130 /* only do color hiliting if we are viewing a message */
1131 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1132 if ((*lineInfo)[n].type == -1) {
1133 /* determine the line class */
1135 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1142 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1143 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1145 /* avoid race condition for continuation lines when scrolling up */
1147 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1149 (*lineInfo)[m].type = (*lineInfo)[n].type;
1152 /* this also prevents searching through the hidden lines */
1153 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1154 flags = 0; /* M_NOSHOW */
1157 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1158 * don't want to compute it every time M_TYPES is set, since this
1159 * would slow down the "bottom" function unacceptably. A compromise
1160 * solution is hence to call regexec() again, just to find out the
1161 * length of the quote prefix.
1163 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1164 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1167 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1173 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1174 (*lineInfo)[n].quote = classify_quote (QuoteList,
1175 (char *) fmt + pmatch[0].rm_so,
1176 pmatch[0].rm_eo - pmatch[0].rm_so,
1177 force_redraw, q_level);
1180 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1181 && (*lineInfo)[n].search_cnt == -1) {
1183 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1191 (*lineInfo)[n].search_cnt = 0;
1193 (SearchRE, (char *) fmt + offset, 1, pmatch,
1194 (offset ? REG_NOTBOL : 0)) == 0) {
1195 if (++((*lineInfo)[n].search_cnt) > 1)
1196 safe_realloc (&((*lineInfo)[n].search),
1197 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1199 (*lineInfo)[n].search = safe_malloc (sizeof (struct syntax_t));
1200 pmatch[0].rm_so += offset;
1201 pmatch[0].rm_eo += offset;
1202 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1204 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1207 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1208 offset++; /* avoid degenerate cases */
1210 offset = pmatch[0].rm_eo;
1216 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1217 /* we've already scanned this line, so just exit */
1220 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1221 /* no need to try to display this line... */
1222 return (1); /* fake display */
1225 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1226 sizeof (buf), &buf_ready)) < 0) {
1232 /* now chose a good place to break the line */
1234 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1236 buf_ptr = buf + cnt;
1238 /* move the break point only if smart_wrap is set */
1239 if (option (OPTWRAP)) {
1241 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1242 && buf[cnt] != '\r') {
1244 /* skip trailing blanks */
1245 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1250 buf_ptr = buf + cnt; /* a very long word... */
1252 if (!(flags & M_PAGER_NSKIP))
1253 /* skip leading blanks on the next line too */
1254 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1258 if (*buf_ptr == '\r')
1260 if (*buf_ptr == '\n')
1263 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1264 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1265 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1267 /* if we don't need to display the line we are done */
1268 if (!(flags & M_SHOW))
1271 /* display the line */
1272 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1274 /* avoid a bug in ncurses... */
1275 #ifndef USE_SLANG_CURSES
1277 SETCOLOR (MT_COLOR_NORMAL);
1282 /* end the last color pattern (needed by S-Lang) */
1283 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1284 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1287 * Fill the blank space at the end of the line with the prevailing color.
1288 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1289 * to make sure to reset the color *after* that
1291 if (flags & M_SHOWCOLOR) {
1292 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1293 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1294 def_color = ((*lineInfo)[m].syntax)[0].color;
1296 def_color = ColorDefs[(*lineInfo)[m].type];
1298 attrset (def_color);
1300 bkgdset (def_color | ' ');
1304 /* ncurses always wraps lines when you get to the right side of the
1305 * screen, but S-Lang seems to only wrap if the next character is *not*
1308 #ifndef USE_SLANG_CURSES
1314 * reset the color back to normal. This *must* come after the
1315 * addch('\n'), otherwise the color for this line will not be
1316 * filled to the right margin.
1318 if (flags & M_SHOWCOLOR) {
1319 SETCOLOR (MT_COLOR_NORMAL);
1320 BKGDSET (MT_COLOR_NORMAL);
1323 /* build a return code */
1324 if (!(flags & M_SHOW))
1330 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1332 while (cur > 0 && nlines > 0) {
1334 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1341 static struct mapping_t PagerHelp[] = {
1342 {N_("Exit"), OP_EXIT},
1343 {N_("PrevPg"), OP_PREV_PAGE},
1344 {N_("NextPg"), OP_NEXT_PAGE},
1347 static struct mapping_t PagerHelpExtra[] = {
1348 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1349 {N_("Del"), OP_DELETE},
1350 {N_("Reply"), OP_REPLY},
1351 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1356 static struct mapping_t PagerNewsHelpExtra[] = {
1357 {N_("Post"), OP_POST},
1358 {N_("Followup"), OP_FOLLOWUP},
1359 {N_("Del"), OP_DELETE},
1360 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1367 /* This pager is actually not so simple as it once was. It now operates in
1368 two modes: one for viewing messages and the other for viewing help. These
1369 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1370 is there so that we can do operations on the current message without the
1371 need to pop back out to the main-menu. */
1373 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1375 static char searchbuf[STRING];
1376 char buffer[LONG_STRING];
1377 char helpstr[SHORT_STRING * 2];
1378 char tmphelp[SHORT_STRING * 2];
1379 int maxLine, lastLine = 0;
1380 struct line_t *lineInfo;
1381 struct q_class_t *QuoteList = NULL;
1382 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1383 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1385 int redraw = REDRAW_FULL;
1387 long last_pos = 0, last_offset = 0;
1388 int old_smart_wrap, old_markers;
1391 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1392 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1394 int bodyoffset = 1; /* offset of first line of real text */
1395 int statusoffset = 0; /* offset for the status bar */
1396 int helpoffset = LINES - 2; /* offset for the help bar. */
1397 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1399 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1400 int indexoffset = 0; /* offset for the PI */
1401 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1402 int indicator = indexlen / 3; /* the indicator line of the PI */
1403 int old_PagerIndexLines; /* some people want to resize it
1404 * while inside the pager... */
1410 if (!(flags & M_SHOWCOLOR))
1411 flags |= M_SHOWFLAT;
1413 if ((fp = fopen (fname, "r")) == NULL) {
1414 mutt_perror (fname);
1418 if (stat (fname, &sb) != 0) {
1419 mutt_perror (fname);
1425 /* Initialize variables */
1427 if (IsHeader (extra) && !extra->hdr->read) {
1428 Context->msgnotreadyet = extra->hdr->msgno;
1429 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1432 lineInfo = safe_malloc (sizeof (struct line_t) * (maxLine = LINES));
1433 for (i = 0; i < maxLine; i++) {
1434 memset (&lineInfo[i], 0, sizeof (struct line_t));
1435 lineInfo[i].type = -1;
1436 lineInfo[i].search_cnt = -1;
1437 lineInfo[i].syntax = safe_malloc (sizeof (struct syntax_t));
1438 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1441 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1442 if (IsHeader (extra)) {
1443 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1444 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1447 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1450 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1453 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1454 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1455 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1465 if (redraw & REDRAW_FULL) {
1466 SETCOLOR (MT_COLOR_NORMAL);
1467 /* clear() doesn't optimize screen redraws */
1471 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1472 indexlen = Context->vcount + 1;
1474 indexlen = PagerIndexLines;
1476 indicator = indexlen / 3;
1478 if (option (OPTSTATUSONTOP)) {
1480 statusoffset = IsHeader (extra) ? indexlen : 0;
1481 bodyoffset = statusoffset + 1;
1482 helpoffset = LINES - 2;
1483 bodylen = helpoffset - bodyoffset;
1484 if (!option (OPTHELP))
1490 statusoffset = LINES - 2;
1491 if (!option (OPTHELP))
1493 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1494 bodylen = statusoffset - bodyoffset;
1497 if (option (OPTHELP)) {
1498 SETCOLOR (MT_COLOR_STATUS);
1499 move (helpoffset, 0);
1500 mutt_paddstr (COLS, helpstr);
1501 SETCOLOR (MT_COLOR_NORMAL);
1504 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1505 if (Resize != NULL) {
1506 if ((SearchCompiled = Resize->SearchCompiled)) {
1508 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1509 SearchFlag = M_SEARCH;
1510 SearchBack = Resize->SearchBack;
1512 lines = Resize->line;
1513 redraw |= REDRAW_SIGWINCH;
1519 if (IsHeader (extra) && PagerIndexLines) {
1520 if (index == NULL) {
1521 /* only allocate the space if/when we need the index.
1522 Initialise the menu as per the main index */
1523 index = mutt_new_menu ();
1524 index->menu = MENU_MAIN;
1525 index->make_entry = index_make_entry;
1526 index->color = index_color;
1527 index->max = Context->vcount;
1528 index->current = extra->hdr->virtual;
1531 SETCOLOR (MT_COLOR_NORMAL);
1532 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1534 index->pagelen = indexlen - 1;
1536 /* some fudge to work out where abouts the indicator should go */
1537 if (index->current - indicator < 0)
1539 else if (index->max - index->current < index->pagelen - indicator)
1540 index->top = index->max - index->pagelen;
1542 index->top = index->current - indicator;
1544 menu_redraw_index (index);
1547 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1551 if (redraw & REDRAW_SIGWINCH) {
1554 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1555 has_types | SearchFlag, &QuoteList, &q_level,
1556 &force_redraw, &SearchRE) == 0) {
1557 if (!lineInfo[i].continuation && ++j == lines) {
1562 redraw |= REDRAW_SIDEBAR;
1566 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1568 move (bodyoffset, SidebarWidth);
1569 curline = oldtopline = topline;
1573 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1574 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1576 (flags & M_DISPLAYFLAGS) | hideQuoted |
1577 SearchFlag, &QuoteList, &q_level, &force_redraw,
1581 move (lines + bodyoffset, SidebarWidth);
1582 redraw |= REDRAW_SIDEBAR;
1584 last_offset = lineInfo[curline].offset;
1585 } while (force_redraw);
1587 SETCOLOR (MT_COLOR_TILDE);
1588 BKGDSET (MT_COLOR_TILDE);
1589 while (lines < bodylen) {
1591 if (option (OPTTILDE))
1595 move (lines + bodyoffset, SidebarWidth);
1597 /* We are going to update the pager status bar, so it isn't
1598 * necessary to reset to normal color now. */
1600 redraw |= REDRAW_STATUS; /* need to update the % seen */
1603 if (redraw & REDRAW_STATUS) {
1604 /* print out the pager status bar */
1605 SETCOLOR (MT_COLOR_STATUS);
1606 BKGDSET (MT_COLOR_STATUS);
1607 CLEARLINE_WIN (statusoffset);
1608 if (IsHeader (extra)) {
1609 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1610 size_t l2 = sizeof (buffer);
1612 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1613 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1615 else if (IsMsgAttach (extra)) {
1616 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1617 size_t l2 = sizeof (buffer);
1619 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1620 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1622 if (option(OPTSTATUSONTOP)) {
1625 /*move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (InHelp?(LINES-2):(indexlen - 1))),
1626 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);*/
1627 mutt_paddstr (COLS - 10 - (option(OPTSTATUSONTOP)?0:SidebarWidth), IsHeader (extra)
1628 || IsMsgAttach (extra) ? buffer : banner);
1631 if (last_pos < sb.st_size - 1)
1632 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1634 addstr (topline == 0 ? "all)" : "end)");
1635 BKGDSET (MT_COLOR_NORMAL);
1636 SETCOLOR (MT_COLOR_NORMAL);
1639 if (redraw & REDRAW_SIDEBAR)
1640 sidebar_draw (MENU_PAGER);
1642 if ((redraw & REDRAW_INDEX) && index) {
1643 /* redraw the pager_index indicator, because the
1644 * flags for this message might have changed. */
1645 menu_redraw_current (index);
1646 sidebar_draw (MENU_PAGER);
1647 /* print out the index status bar */
1648 menu_status_line (buffer, sizeof (buffer), index, NONULL (Status));
1649 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)),
1650 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);
1651 SETCOLOR (MT_COLOR_STATUS);
1652 mutt_paddstr (COLS - (option (OPTSTATUSONTOP) ? 0 : SidebarWidth),
1654 SETCOLOR (MT_COLOR_NORMAL);
1656 /* if we're not using the index, update every time */
1658 sidebar_draw (MENU_PAGER);
1662 move (statusoffset, COLS - 1);
1664 ch = km_dokey (MENU_PAGER);
1666 mutt_clear_error ();
1673 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1674 else if (SigWinch) {
1675 mutt_resize_screen ();
1677 /* Store current position. */
1679 for (i = 0; i <= topline; i++)
1680 if (!lineInfo[i].continuation)
1683 if (flags & M_PAGER_RETWINCH) {
1684 Resize = safe_malloc (sizeof (struct resize));
1686 Resize->line = lines;
1687 Resize->SearchCompiled = SearchCompiled;
1688 Resize->SearchBack = SearchBack;
1691 rc = OP_REFORMAT_WINCH;
1694 for (i = 0; i < maxLine; i++) {
1695 lineInfo[i].offset = 0;
1696 lineInfo[i].type = -1;
1697 lineInfo[i].continuation = 0;
1698 lineInfo[i].chunks = 0;
1699 lineInfo[i].search_cnt = -1;
1700 lineInfo[i].quote = NULL;
1702 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
1703 if (SearchCompiled && lineInfo[i].search)
1704 FREE (&(lineInfo[i].search));
1710 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1715 clearok (stdscr, TRUE); /*force complete redraw */
1719 else if (ch == -1) {
1733 if (lineInfo[curline].offset < sb.st_size - 1) {
1734 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1736 else if (option (OPTPAGERSTOP)) {
1737 /* emulate "less -q" and don't go on to the next message. */
1738 mutt_error _("Bottom of message is shown.");
1741 /* end of the current message, so display the next message. */
1742 rc = OP_MAIN_NEXT_UNDELETED;
1750 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1753 mutt_error _("Top of message is shown.");
1757 if (lineInfo[curline].offset < sb.st_size - 1) {
1760 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1766 mutt_error _("Bottom of message is shown.");
1771 topline = upNLines (1, lineInfo, topline, hideQuoted);
1773 mutt_error _("Top of message is shown.");
1780 mutt_error _("Top of message is shown.");
1785 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1787 mutt_error _("Top of message is shown.");
1791 if (lineInfo[curline].offset < sb.st_size - 1) {
1792 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1794 else if (option (OPTPAGERSTOP)) {
1795 /* emulate "less -q" and don't go on to the next message. */
1796 mutt_error _("Bottom of message is shown.");
1799 /* end of the current message, so display the next message. */
1800 rc = OP_MAIN_NEXT_UNDELETED;
1805 case OP_SEARCH_NEXT:
1806 case OP_SEARCH_OPPOSITE:
1807 if (SearchCompiled) {
1809 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1810 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1811 /* searching forward */
1812 for (i = topline + 1; i < lastLine; i++) {
1813 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1814 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1821 mutt_error _("Not found.");
1824 /* searching backward */
1825 for (i = topline - 1; i >= 0; i--) {
1826 if ((!hideQuoted || (has_types &&
1827 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1828 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1835 mutt_error _("Not found.");
1838 if (lineInfo[topline].search_cnt > 0)
1839 SearchFlag = M_SEARCH;
1843 /* no previous search pattern, so fall through to search */
1846 case OP_SEARCH_REVERSE:
1847 strfcpy (buffer, searchbuf, sizeof (buffer));
1848 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1849 _("Search: ")), buffer, sizeof (buffer),
1853 if (!strcmp (buffer, searchbuf)) {
1854 if (SearchCompiled) {
1855 /* do an implicit search-next */
1856 if (ch == OP_SEARCH)
1857 ch = OP_SEARCH_NEXT;
1859 ch = OP_SEARCH_OPPOSITE;
1868 strfcpy (searchbuf, buffer, sizeof (searchbuf));
1870 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1871 if (ch == OP_SEARCH)
1873 else if (ch == OP_SEARCH_REVERSE)
1876 if (SearchCompiled) {
1877 regfree (&SearchRE);
1878 for (i = 0; i < lastLine; i++) {
1879 if (lineInfo[i].search)
1880 FREE (&(lineInfo[i].search));
1881 lineInfo[i].search_cnt = -1;
1886 REGCOMP (&SearchRE, searchbuf,
1887 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1888 regerror (err, &SearchRE, buffer, sizeof (buffer));
1889 mutt_error ("%s", buffer);
1890 regfree (&SearchRE);
1891 for (i = 0; i < maxLine; i++) {
1893 if (lineInfo[i].search)
1894 FREE (&(lineInfo[i].search));
1895 lineInfo[i].search_cnt = -1;
1902 /* update the search pointers */
1904 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1905 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1906 &QuoteList, &q_level,
1907 &force_redraw, &SearchRE) == 0) {
1909 redraw |= REDRAW_SIDEBAR;
1913 /* searching forward */
1914 for (i = topline; i < lastLine; i++) {
1915 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1916 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1924 /* searching backward */
1925 for (i = topline; i >= 0; i--) {
1926 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1927 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1935 if (lineInfo[topline].search_cnt == 0) {
1937 mutt_error _("Not found.");
1940 SearchFlag = M_SEARCH;
1942 redraw = REDRAW_BODY;
1945 case OP_SEARCH_TOGGLE:
1946 if (SearchCompiled) {
1947 SearchFlag ^= M_SEARCH;
1948 redraw = REDRAW_BODY;
1953 /* don't let the user enter the help-menu from the help screen! */
1956 mutt_help (MENU_PAGER);
1957 redraw = REDRAW_FULL;
1961 mutt_error _("Help is currently being shown.");
1964 case OP_PAGER_HIDE_QUOTED:
1966 hideQuoted ^= M_HIDE;
1967 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1968 topline = upNLines (1, lineInfo, topline, hideQuoted);
1970 redraw = REDRAW_BODY;
1974 case OP_PAGER_SKIP_QUOTED:
1977 int new_topline = topline;
1979 while ((new_topline < lastLine ||
1980 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1981 new_topline, &lastLine,
1982 &maxLine, M_TYPES, &QuoteList,
1983 &q_level, &force_redraw,
1985 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
1986 redraw |= REDRAW_SIDEBAR;
1991 mutt_error _("No more quoted text.");
1996 while ((new_topline < lastLine ||
1997 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1998 new_topline, &lastLine,
1999 &maxLine, M_TYPES, &QuoteList,
2000 &q_level, &force_redraw,
2002 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2004 redraw |= REDRAW_SIDEBAR;
2008 mutt_error _("No more unquoted text after quoted text.");
2012 topline = new_topline;
2016 case OP_PAGER_BOTTOM: /* move to the end of the file */
2017 if (lineInfo[curline].offset < sb.st_size - 1) {
2019 /* make sure the types are defined to the end of file */
2020 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2021 &maxLine, has_types,
2022 &QuoteList, &q_level, &force_redraw,
2025 redraw |= REDRAW_SIDEBAR;
2027 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2030 mutt_error _("Bottom of message is shown.");
2034 clearok (stdscr, TRUE);
2035 redraw = REDRAW_FULL;
2039 km_error_key (MENU_PAGER);
2042 /* --------------------------------------------------------------------
2043 * The following are operations on the current message rather than
2044 * adjusting the view of the message.
2047 case OP_BOUNCE_MESSAGE:
2048 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2050 if (IsMsgAttach (extra))
2051 mutt_attach_bounce (extra->fp, extra->hdr,
2052 extra->idx, extra->idxlen, extra->bdy);
2054 ci_bounce_message (extra->hdr, &redraw);
2058 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2060 if (IsMsgAttach (extra))
2061 mutt_attach_resend (extra->fp, extra->hdr,
2062 extra->idx, extra->idxlen, extra->bdy);
2064 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2065 redraw = REDRAW_FULL;
2068 case OP_CHECK_TRADITIONAL:
2069 CHECK_MODE (IsHeader (extra));
2070 if (!(WithCrypto & APPLICATION_PGP))
2072 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2074 rc = OP_CHECK_TRADITIONAL;
2078 case OP_CREATE_ALIAS:
2079 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2080 if (IsMsgAttach (extra))
2081 mutt_create_alias (extra->bdy->hdr->env, NULL);
2083 mutt_create_alias (extra->hdr->env, NULL);
2084 MAYBE_REDRAW (redraw);
2087 case OP_PURGE_MESSAGE:
2089 CHECK_MODE (IsHeader (extra));
2092 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2094 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2095 mutt_set_flag (Context, extra->hdr, M_PURGED,
2096 ch != OP_PURGE_MESSAGE ? 0 : 1);
2097 if (option (OPTDELETEUNTAG))
2098 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2099 redraw = REDRAW_STATUS | REDRAW_INDEX;
2100 if (option (OPTRESOLVE)) {
2102 rc = OP_MAIN_NEXT_UNDELETED;
2106 case OP_DELETE_THREAD:
2107 case OP_DELETE_SUBTHREAD:
2108 CHECK_MODE (IsHeader (extra));
2111 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2113 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2114 ch == OP_DELETE_THREAD ? 0 : 1);
2117 if (option (OPTDELETEUNTAG))
2118 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2119 ch == OP_DELETE_THREAD ? 0 : 1);
2120 if (option (OPTRESOLVE)) {
2121 rc = OP_MAIN_NEXT_UNDELETED;
2125 if (!option (OPTRESOLVE) && PagerIndexLines)
2126 redraw = REDRAW_FULL;
2128 redraw = REDRAW_STATUS | REDRAW_INDEX;
2132 case OP_DISPLAY_ADDRESS:
2133 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2134 if (IsMsgAttach (extra))
2135 mutt_display_address (extra->bdy->hdr->env);
2137 mutt_display_address (extra->hdr->env);
2140 case OP_ENTER_COMMAND:
2141 old_smart_wrap = option (OPTWRAP);
2142 old_markers = option (OPTMARKERS);
2143 old_PagerIndexLines = PagerIndexLines;
2145 CurrentMenu = MENU_PAGER;
2146 mutt_enter_command ();
2148 if (option (OPTNEEDRESORT)) {
2149 unset_option (OPTNEEDRESORT);
2150 CHECK_MODE (IsHeader (extra));
2151 set_option (OPTNEEDRESORT);
2154 if (old_PagerIndexLines != PagerIndexLines) {
2156 mutt_menuDestroy (&index);
2160 if (option (OPTWRAP) != old_smart_wrap ||
2161 option (OPTMARKERS) != old_markers) {
2162 if (flags & M_PAGER_RETWINCH) {
2164 rc = OP_REFORMAT_WINCH;
2168 /* count the real lines above */
2170 for (i = 0; i <= topline; i++) {
2171 if (!lineInfo[i].continuation)
2175 /* we need to restart the whole thing */
2176 for (i = 0; i < maxLine; i++) {
2177 lineInfo[i].offset = 0;
2178 lineInfo[i].type = -1;
2179 lineInfo[i].continuation = 0;
2180 lineInfo[i].chunks = 0;
2181 lineInfo[i].search_cnt = -1;
2182 lineInfo[i].quote = NULL;
2184 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2185 if (SearchCompiled && lineInfo[i].search)
2186 FREE (&(lineInfo[i].search));
2189 if (SearchCompiled) {
2190 regfree (&SearchRE);
2195 /* try to keep the old position */
2198 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2199 &lastLine, &maxLine,
2200 (has_types ? M_TYPES : 0),
2201 &QuoteList, &q_level, &force_redraw,
2203 redraw |= REDRAW_SIDEBAR;
2204 if (!lineInfo[topline].continuation)
2213 if (option (OPTFORCEREDRAWPAGER))
2214 redraw = REDRAW_FULL;
2215 unset_option (OPTFORCEREDRAWINDEX);
2216 unset_option (OPTFORCEREDRAWPAGER);
2219 case OP_FLAG_MESSAGE:
2220 CHECK_MODE (IsHeader (extra));
2223 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2225 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2226 redraw = REDRAW_STATUS | REDRAW_INDEX;
2227 if (option (OPTRESOLVE)) {
2229 rc = OP_MAIN_NEXT_UNDELETED;
2234 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2235 if (IsAttach (extra))
2236 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2238 mutt_pipe_message (extra->hdr);
2239 MAYBE_REDRAW (redraw);
2243 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2244 if (IsAttach (extra))
2245 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2247 mutt_print_message (extra->hdr);
2251 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2253 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2254 redraw = REDRAW_FULL;
2259 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2261 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2262 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2263 query_quadoption (OPT_TOMODERATED,
2265 ("Posting to this group not allowed, may be moderated. Continue?"))
2268 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2269 redraw = REDRAW_FULL;
2272 case OP_FORWARD_TO_GROUP:
2273 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2275 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2276 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2277 query_quadoption (OPT_TOMODERATED,
2279 ("Posting to this group not allowed, may be moderated. Continue?"))
2282 if (IsMsgAttach (extra))
2283 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2284 extra->idxlen, extra->bdy, SENDNEWS);
2286 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2288 redraw = REDRAW_FULL;
2292 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2295 if (IsMsgAttach (extra))
2296 followup_to = extra->bdy->hdr->env->followup_to;
2298 followup_to = extra->hdr->env->followup_to;
2300 if (!followup_to || safe_strcasecmp (followup_to, "poster") ||
2301 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2302 _("Reply by mail as poster prefers?")) != M_YES) {
2303 if (extra->ctx && extra->ctx->magic == M_NNTP
2304 && !((NNTP_DATA *) extra->ctx->data)->allowed
2305 && query_quadoption (OPT_TOMODERATED,
2307 ("Posting to this group not allowed, may be moderated. Continue?"))
2310 if (IsMsgAttach (extra))
2311 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2312 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2314 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2315 extra->ctx, extra->hdr);
2316 redraw = REDRAW_FULL;
2322 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2324 if (IsMsgAttach (extra))
2325 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2326 extra->idxlen, extra->bdy, SENDREPLY);
2328 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2329 redraw = REDRAW_FULL;
2332 case OP_RECALL_MESSAGE:
2333 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2335 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2336 redraw = REDRAW_FULL;
2339 case OP_GROUP_REPLY:
2340 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2342 if (IsMsgAttach (extra))
2343 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2344 extra->idxlen, extra->bdy,
2345 SENDREPLY | SENDGROUPREPLY);
2347 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2349 redraw = REDRAW_FULL;
2353 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2355 if (IsMsgAttach (extra))
2356 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2357 extra->idxlen, extra->bdy,
2358 SENDREPLY | SENDLISTREPLY);
2360 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2362 redraw = REDRAW_FULL;
2365 case OP_FORWARD_MESSAGE:
2366 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2368 if (IsMsgAttach (extra))
2369 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2370 extra->idxlen, extra->bdy, 0);
2372 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2373 redraw = REDRAW_FULL;
2376 case OP_DECRYPT_SAVE:
2383 if (IsAttach (extra)) {
2384 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2389 case OP_COPY_MESSAGE:
2390 case OP_DECODE_SAVE:
2391 case OP_DECODE_COPY:
2392 case OP_DECRYPT_COPY:
2393 if (!WithCrypto && ch == OP_DECRYPT_COPY) {
2397 CHECK_MODE (IsHeader (extra));
2398 if (mutt_save_message (extra->hdr,
2399 (ch == OP_DECRYPT_SAVE) ||
2400 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2401 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2402 (ch == OP_DECRYPT_SAVE)
2403 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2404 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2405 || ch == OP_DECRYPT_SAVE)) {
2406 if (option (OPTRESOLVE)) {
2408 rc = OP_MAIN_NEXT_UNDELETED;
2411 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2413 MAYBE_REDRAW (redraw);
2416 case OP_SHELL_ESCAPE:
2417 mutt_shell_escape ();
2418 MAYBE_REDRAW (redraw);
2422 CHECK_MODE (IsHeader (extra));
2423 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2425 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2426 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2427 ? NULL : Context->last_tag);
2429 redraw = REDRAW_STATUS | REDRAW_INDEX;
2430 if (option (OPTRESOLVE)) {
2437 CHECK_MODE (IsHeader (extra));
2440 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2442 if (extra->hdr->read || extra->hdr->old)
2443 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2445 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2447 Context->msgnotreadyet = -1;
2448 redraw = REDRAW_STATUS | REDRAW_INDEX;
2449 if (option (OPTRESOLVE)) {
2451 rc = OP_MAIN_NEXT_UNDELETED;
2456 CHECK_MODE (IsHeader (extra));
2459 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2461 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2462 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2463 redraw = REDRAW_STATUS | REDRAW_INDEX;
2464 if (option (OPTRESOLVE)) {
2470 case OP_UNDELETE_THREAD:
2471 case OP_UNDELETE_SUBTHREAD:
2472 CHECK_MODE (IsHeader (extra));
2475 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2477 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2478 ch == OP_UNDELETE_THREAD ? 0 : 1)
2479 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2480 ch == OP_UNDELETE_THREAD ? 0 : 1);
2483 if (option (OPTRESOLVE)) {
2484 rc = (ch == OP_DELETE_THREAD) ?
2485 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2489 if (!option (OPTRESOLVE) && PagerIndexLines)
2490 redraw = REDRAW_FULL;
2492 redraw = REDRAW_STATUS | REDRAW_INDEX;
2502 redraw |= REDRAW_SIDEBAR;
2505 case OP_VIEW_ATTACHMENTS:
2506 if (flags & M_PAGER_ATTACHMENT) {
2508 rc = OP_ATTACH_COLLAPSE;
2511 CHECK_MODE (IsHeader (extra));
2512 mutt_view_attachments (extra->hdr);
2513 if (extra->hdr->attach_del)
2514 Context->changed = 1;
2515 redraw = REDRAW_FULL;
2520 if (!(WithCrypto & APPLICATION_PGP)) {
2524 CHECK_MODE (IsHeader (extra));
2526 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2527 redraw = REDRAW_FULL;
2531 case OP_FORGET_PASSPHRASE:
2532 crypt_forget_passphrase ();
2535 case OP_EXTRACT_KEYS:
2540 CHECK_MODE (IsHeader (extra));
2541 crypt_extract_keys_from_messages (extra->hdr);
2542 redraw = REDRAW_FULL;
2545 case OP_SIDEBAR_SCROLL_UP:
2546 case OP_SIDEBAR_SCROLL_DOWN:
2547 case OP_SIDEBAR_NEXT:
2548 case OP_SIDEBAR_NEXT_NEW:
2549 case OP_SIDEBAR_PREV:
2550 case OP_SIDEBAR_PREV_NEW:
2551 sidebar_scroll (ch, MENU_PAGER);
2560 if (IsHeader (extra))
2561 Context->msgnotreadyet = -1;
2563 cleanup_quote (&QuoteList);
2565 for (i = 0; i < maxLine; i++) {
2566 FREE (&(lineInfo[i].syntax));
2567 if (SearchCompiled && lineInfo[i].search)
2568 FREE (&(lineInfo[i].search));
2570 if (SearchCompiled) {
2571 regfree (&SearchRE);
2576 mutt_menuDestroy (&index);
2577 return (rc != -1 ? rc : 0);