2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 #include "mutt_curses.h"
25 #include "mutt_regex.h"
27 #include "mutt_menu.h"
38 #include "imap_private.h"
41 #include "mutt_crypt.h"
50 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
52 #define IsAttach(x) (x && (x)->bdy)
53 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
54 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
55 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
56 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
58 static const char *Not_available_in_this_menu = 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 = N_("Function not permitted in attach-message mode.");
62 #define CHECK_MODE(x) if (!(x)) \
65 mutt_error _(Not_available_in_this_menu); \
69 #define CHECK_READONLY if (Context->readonly) \
72 mutt_error _(Mailbox_is_read_only); \
76 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
79 mutt_error _(Function_not_permitted_in_attach_message_mode); \
84 /* the error message returned here could be better. */
85 #define CHECK_IMAP_ACL(aclbit) if (Context->magic == M_IMAP) \
86 if (mutt_bit_isset (((IMAP_DATA *)Context->data)->capabilities, ACL) \
87 && !mutt_bit_isset(((IMAP_DATA *)Context->data)->rights,aclbit)){ \
89 mutt_error ("Operation not permitted by the IMAP ACL for this mailbox"); \
100 struct q_class_t *next, *prev;
101 struct q_class_t *down, *up;
118 struct syntax_t *syntax;
119 struct syntax_t *search;
120 struct q_class_t *quote;
123 #define ANSI_OFF (1<<0)
124 #define ANSI_BLINK (1<<1)
125 #define ANSI_BOLD (1<<2)
126 #define ANSI_UNDERLINE (1<<3)
127 #define ANSI_REVERSE (1<<4)
128 #define ANSI_COLOR (1<<5)
130 typedef struct _ansi_attr {
137 static short InHelp = 0;
139 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
140 static struct resize {
147 #define NumSigLines 4
149 static int check_sig (const char *s, struct line_t *info, int n)
153 while (n > 0 && count <= NumSigLines)
155 if (info[n].type != MT_COLOR_SIGNATURE)
164 if (count > NumSigLines)
166 /* check for a blank line */
181 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags, int special,
184 int def_color; /* color without syntax hilight */
185 int color; /* final color */
186 static int last_color; /* last color set */
187 int search = 0, i, m;
190 last_color = -1; /* force attrset() */
192 if (lineInfo[n].continuation)
194 if (!cnt && option (OPTMARKERS))
196 SETCOLOR (MT_COLOR_MARKERS);
198 last_color = ColorDefs[MT_COLOR_MARKERS];
200 m = (lineInfo[n].syntax)[0].first;
201 cnt += (lineInfo[n].syntax)[0].last;
205 if (!(flags & M_SHOWCOLOR))
206 def_color = ColorDefs[MT_COLOR_NORMAL];
207 else if (lineInfo[m].type == MT_COLOR_HEADER)
208 def_color = (lineInfo[m].syntax)[0].color;
210 def_color = ColorDefs[lineInfo[m].type];
212 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED)
214 struct q_class_t *class = lineInfo[m].quote;
218 def_color = class->color;
220 while (class && class->length > cnt)
222 def_color = class->color;
229 if (flags & M_SHOWCOLOR)
231 for (i = 0; i < lineInfo[m].chunks; i++)
233 /* we assume the chunks are sorted */
234 if (cnt > (lineInfo[m].syntax)[i].last)
236 if (cnt < (lineInfo[m].syntax)[i].first)
238 if (cnt != (lineInfo[m].syntax)[i].last)
240 color = (lineInfo[m].syntax)[i].color;
243 /* don't break here, as cnt might be
244 * in the next chunk as well */
248 if (flags & M_SEARCH)
250 for (i = 0; i < lineInfo[m].search_cnt; i++)
252 if (cnt > (lineInfo[m].search)[i].last)
254 if (cnt < (lineInfo[m].search)[i].first)
256 if (cnt != (lineInfo[m].search)[i].last)
258 color = ColorDefs[MT_COLOR_SEARCH];
265 /* handle "special" bold & underlined characters */
266 if (special || a->attr)
269 if ((a->attr & ANSI_COLOR))
272 a->pair = mutt_alloc_color (a->fg, a->bg);
274 if (a->attr & ANSI_BOLD)
279 if ((special & A_BOLD) || (a->attr & ANSI_BOLD))
281 if (ColorDefs[MT_COLOR_BOLD] && !search)
282 color = ColorDefs[MT_COLOR_BOLD];
286 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE))
288 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
289 color = ColorDefs[MT_COLOR_UNDERLINE];
291 color ^= A_UNDERLINE;
293 else if (a->attr & ANSI_REVERSE)
297 else if (a->attr & ANSI_BLINK)
301 else if (a->attr & ANSI_OFF)
307 if (color != last_color)
315 append_line (struct line_t *lineInfo, int n, int cnt)
319 lineInfo[n+1].type = lineInfo[n].type;
320 (lineInfo[n+1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
321 lineInfo[n+1].continuation = 1;
323 /* find the real start of the line */
324 for (m = n; m >= 0; m--)
325 if (lineInfo[m].continuation == 0) break;
327 (lineInfo[n+1].syntax)[0].first = m;
328 (lineInfo[n+1].syntax)[0].last = (lineInfo[n].continuation) ?
329 cnt + (lineInfo[n].syntax)[0].last : cnt;
333 new_class_color (struct q_class_t *class, int *q_level)
335 class->index = (*q_level)++;
336 class->color = ColorQuote[class->index % ColorQuoteUsed];
340 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
341 int index, int *q_level)
343 struct q_class_t * q_list;
346 new_class->index = -1;
350 if (q_list->index >= index)
353 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
356 q_list = q_list->down;
357 else if (q_list->next)
358 q_list = q_list->next;
361 while (!q_list->next)
368 q_list = q_list->next;
372 new_class->index = index;
373 new_class->color = ColorQuote[index % ColorQuoteUsed];
378 cleanup_quote (struct q_class_t **QuoteList)
380 struct q_class_t *ptr;
384 if ((*QuoteList)->down)
385 cleanup_quote (&((*QuoteList)->down));
386 ptr = (*QuoteList)->next;
387 if ((*QuoteList)->prefix)
388 FREE (&(*QuoteList)->prefix);
396 static struct q_class_t *
397 classify_quote (struct q_class_t **QuoteList, const char *qptr,
398 int length, int *force_redraw, int *q_level)
400 struct q_class_t *q_list = *QuoteList;
401 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
403 int offset, tail_lng;
406 if (ColorQuoteUsed <= 1)
408 /* not much point in classifying quotes... */
410 if (*QuoteList == NULL)
412 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
413 class->color = ColorQuote[0];
419 /* Did I mention how much I like emulating Lisp in C? */
421 /* classify quoting prefix */
424 if (length <= q_list->length)
426 /* case 1: check the top level nodes */
428 if (mutt_strncmp (qptr, q_list->prefix, length) == 0)
430 if (length == q_list->length)
431 return q_list; /* same prefix: return the current class */
433 /* found shorter prefix */
436 /* add a node above q_list */
437 tmp = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
438 tmp->prefix = (char *) safe_calloc (1, length + 1);
439 strncpy (tmp->prefix, qptr, length);
440 tmp->length = length;
442 /* replace q_list by tmp in the top level list */
445 tmp->next = q_list->next;
446 q_list->next->prev = tmp;
450 tmp->prev = q_list->prev;
451 q_list->prev->next = tmp;
454 /* make q_list a child of tmp */
458 /* q_list has no siblings for now */
462 /* update the root if necessary */
463 if (q_list == *QuoteList)
466 index = q_list->index;
468 /* tmp should be the return class too */
471 /* next class to test; if tmp is a shorter prefix for another
472 * node, that node can only be in the top level list, so don't
473 * go down after this point
479 /* found another branch for which tmp is a shorter prefix */
481 /* save the next sibling for later */
484 /* unlink q_list from the top level list */
486 q_list->next->prev = q_list->prev;
488 q_list->prev->next = q_list->next;
490 /* at this point, we have a tmp->down; link q_list to it */
492 /* sibling order is important here, q_list should be linked last */
500 index = q_list->index;
502 /* next class to test; as above, we shouldn't go down */
506 /* we found a shorter prefix, so certain quotes have changed classes */
512 /* shorter, but not a substring of the current class: try next */
513 q_list = q_list->next;
519 /* case 2: try subclassing the current top level node */
521 /* tmp != NULL means we already found a shorter prefix at case 1 */
522 if (tmp == NULL && mutt_strncmp (qptr, q_list->prefix, q_list->length) == 0)
524 /* ok, it's a subclass somewhere on this branch */
527 offset = q_list->length;
529 q_list = q_list->down;
530 tail_lng = length - offset;
531 tail_qptr = (char *) qptr + offset;
535 if (length <= q_list->length)
537 if (mutt_strncmp (tail_qptr, (q_list->prefix) + offset, tail_lng) == 0)
539 /* same prefix: return the current class */
540 if (length == q_list->length)
543 /* found shorter common prefix */
546 /* add a node above q_list */
547 tmp = (struct q_class_t *) safe_calloc (1,
548 sizeof (struct q_class_t));
549 tmp->prefix = (char *) safe_calloc (1, length + 1);
550 strncpy (tmp->prefix, qptr, length);
551 tmp->length = length;
553 /* replace q_list by tmp */
556 tmp->next = q_list->next;
557 q_list->next->prev = tmp;
561 tmp->prev = q_list->prev;
562 q_list->prev->next = tmp;
565 /* make q_list a child of tmp */
567 tmp->up = q_list->up;
569 if (tmp->up->down == q_list)
572 /* q_list has no siblings */
576 index = q_list->index;
578 /* tmp should be the return class too */
581 /* next class to test */
586 /* found another branch for which tmp is a shorter prefix */
588 /* save the next sibling for later */
591 /* unlink q_list from the top level list */
593 q_list->next->prev = q_list->prev;
595 q_list->prev->next = q_list->next;
597 /* at this point, we have a tmp->down; link q_list to it */
606 index = q_list->index;
608 /* next class to test */
612 /* we found a shorter prefix, so we need a redraw */
618 q_list = q_list->next;
624 /* longer than the current prefix: try subclassing it */
625 if (tmp == NULL && mutt_strncmp (tail_qptr, (q_list->prefix) + offset,
626 q_list->length - offset) == 0)
628 /* still a subclass: go down one level */
630 offset = q_list->length;
632 q_list = q_list->down;
633 tail_lng = length - offset;
634 tail_qptr = (char *) qptr + offset;
640 /* nope, try the next prefix */
641 q_list = q_list->next;
647 /* still not found so far: add it as a sibling to the current node */
650 tmp = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
651 tmp->prefix = (char *) safe_calloc (1, length + 1);
652 strncpy (tmp->prefix, qptr, length);
653 tmp->length = length;
657 tmp->next = ptr->down;
658 ptr->down->prev = tmp;
663 new_class_color (tmp, q_level);
670 shift_class_colors (*QuoteList, tmp, index, q_level);
677 /* nope, try the next prefix */
678 q_list = q_list->next;
686 /* not found so far: add it as a top level class */
687 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
688 class->prefix = (char *) safe_calloc (1, length + 1);
689 strncpy (class->prefix, qptr, length);
690 class->length = length;
691 new_class_color (class, q_level);
695 class->next = *QuoteList;
696 (*QuoteList)->prev = class;
702 shift_class_colors (*QuoteList, tmp, index, q_level);
707 static int check_attachment_marker (char *);
710 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
711 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
714 COLOR_LINE *color_line;
715 regmatch_t pmatch[1], smatch[1];
716 int found, offset, null_rx, i;
718 if (n == 0 || ISHEADER (lineInfo[n-1].type))
721 lineInfo[n].type = MT_COLOR_NORMAL;
722 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t'))
724 lineInfo[n].type = lineInfo[n-1].type; /* wrapped line */
725 (lineInfo[n].syntax)[0].color = (lineInfo[n-1].syntax)[0].color;
729 lineInfo[n].type = MT_COLOR_HDEFAULT;
730 color_line = ColorHdrList;
733 if (REGEXEC (color_line->rx, buf) == 0)
735 lineInfo[n].type = MT_COLOR_HEADER;
736 lineInfo[n].syntax[0].color = color_line->pair;
739 color_line = color_line->next;
743 else if (mutt_strncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
744 lineInfo[n].type = MT_COLOR_NORMAL;
746 else if (mutt_strncmp ("[-- ", buf, 4) == 0)
747 lineInfo[n].type = MT_COLOR_ATTACHMENT;
749 else if (check_attachment_marker ((char *) raw) == 0)
750 lineInfo[n].type = MT_COLOR_ATTACHMENT;
752 else if (mutt_strcmp ("-- \n", buf) == 0 || mutt_strcmp ("-- \r\n", buf) == 0)
756 lineInfo[n].type = MT_COLOR_SIGNATURE;
757 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
758 (lineInfo[i].type == MT_COLOR_NORMAL ||
759 lineInfo[i].type == MT_COLOR_QUOTED ||
760 lineInfo[i].type == MT_COLOR_HEADER))
763 if (lineInfo[i].chunks)
765 lineInfo[i].chunks = 0;
766 safe_realloc (&(lineInfo[n].syntax),
767 sizeof (struct syntax_t));
769 lineInfo[i++].type = MT_COLOR_SIGNATURE;
772 else if (check_sig (buf, lineInfo, n - 1) == 0)
773 lineInfo[n].type = MT_COLOR_SIGNATURE;
774 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0)
776 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0)
778 if (smatch[0].rm_so > 0)
782 /* hack to avoid making an extra copy of buf */
783 c = buf[smatch[0].rm_so];
784 buf[smatch[0].rm_so] = 0;
786 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0)
788 if (q_classify && lineInfo[n].quote == NULL)
789 lineInfo[n].quote = classify_quote (QuoteList,
790 buf + pmatch[0].rm_so,
791 pmatch[0].rm_eo - pmatch[0].rm_so,
792 force_redraw, q_level);
793 lineInfo[n].type = MT_COLOR_QUOTED;
796 lineInfo[n].type = MT_COLOR_NORMAL;
798 buf[smatch[0].rm_so] = c;
801 lineInfo[n].type = MT_COLOR_NORMAL;
805 if (q_classify && lineInfo[n].quote == NULL)
806 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
807 pmatch[0].rm_eo - pmatch[0].rm_so,
808 force_redraw, q_level);
809 lineInfo[n].type = MT_COLOR_QUOTED;
813 lineInfo[n].type = MT_COLOR_NORMAL;
816 if (lineInfo[n].type == MT_COLOR_NORMAL ||
817 lineInfo[n].type == MT_COLOR_QUOTED)
822 lineInfo[n].chunks = 0;
830 color_line = ColorBodyList;
833 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
834 (offset ? REG_NOTBOL : 0)) == 0)
836 if (pmatch[0].rm_eo != pmatch[0].rm_so)
840 if (++(lineInfo[n].chunks) > 1)
841 safe_realloc (&(lineInfo[n].syntax),
842 (lineInfo[n].chunks) * sizeof (struct syntax_t));
844 i = lineInfo[n].chunks - 1;
845 pmatch[0].rm_so += offset;
846 pmatch[0].rm_eo += offset;
848 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
849 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
850 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last))
852 (lineInfo[n].syntax)[i].color = color_line->pair;
853 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
854 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
860 null_rx = 1; /* empty regexp; don't add it, but keep looking */
862 color_line = color_line->next;
866 offset++; /* avoid degenerate cases */
868 offset = (lineInfo[n].syntax)[i].last;
869 } while (found || null_rx);
873 static int is_ansi (unsigned char *buf)
875 while (*buf && (isdigit(*buf) || *buf == ';'))
877 return (*buf == 'm');
880 static int check_attachment_marker (char *p)
882 char *q = AttachmentMarker;
884 for (;*p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++)
886 return (int) (*p - *q);
889 static int grok_ansi(unsigned char *buf, int pos, ansi_attr *a)
893 while (isdigit(buf[x]) || buf[x] == ';')
896 /* Character Attributes */
897 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm')
903 mutt_free_color (a->fg, a->bg);
910 if (buf[pos] == '1' && (pos+1 == x || buf[pos+1] == ';'))
912 a->attr |= ANSI_BOLD;
915 else if (buf[pos] == '4' && (pos+1 == x || buf[pos+1] == ';'))
917 a->attr |= ANSI_UNDERLINE;
920 else if (buf[pos] == '5' && (pos+1 == x || buf[pos+1] == ';'))
922 a->attr |= ANSI_BLINK;
925 else if (buf[pos] == '7' && (pos+1 == x || buf[pos+1] == ';'))
927 a->attr |= ANSI_REVERSE;
930 else if (buf[pos] == '0' && (pos+1 == x || buf[pos+1] == ';'))
934 mutt_free_color(a->fg,a->bg);
940 else if (buf[pos] == '3' && isdigit(buf[pos+1]))
944 mutt_free_color(a->fg,a->bg);
947 a->attr |= ANSI_COLOR;
948 a->fg = buf[pos+1] - '0';
951 else if (buf[pos] == '4' && isdigit(buf[pos+1]))
955 mutt_free_color(a->fg,a->bg);
958 a->attr |= ANSI_COLOR;
959 a->bg = buf[pos+1] - '0';
964 while (pos < x && buf[pos] != ';') pos++;
974 fill_buffer (FILE *f, long *last_pos, long offset, unsigned char *buf,
975 unsigned char *fmt, size_t blen, int *buf_ready)
978 static int b_read = 0;
983 if (offset != *last_pos)
984 fseek (f, offset, 0);
985 if (fgets ((char *) buf, blen - 1, f) == NULL)
990 *last_pos = ftell (f);
991 b_read = (int) (*last_pos - offset);
994 /* copy "buf" to "fmt", but without bold and underline controls */
998 if (*p == '\010' && (p > buf))
1000 if (*(p+1) == '_') /* underline */
1002 else if (*(p+1)) /* bold or overstrike */
1010 else if (*p == '\033' && *(p+1) == '[' && is_ansi (p + 2))
1012 while (*p++ != 'm') /* skip ANSI sequence */
1015 else if (*p == '\033' && *(p+1) == ']' && check_attachment_marker ((char *) p) == 0)
1017 dprint (2, (debugfile, "fill_buffer: Seen attachment marker.\n"));
1018 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
1035 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
1036 int flags, ansi_attr *pa, int cnt,
1037 int *pspace, int *pvch, int *pcol, int *pspecial)
1039 int space = -1; /* index of the last space or TAB */
1040 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
1041 int ch, vch, k, last_special = -1, special = 0, t;
1045 int wrap_cols = COLS - WrapMargin - SidebarWidth;
1050 /* FIXME: this should come from lineInfo */
1051 memset(&mbstate, 0, sizeof(mbstate));
1053 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k)
1055 /* Handle ANSI sequences */
1056 while (cnt-ch >= 2 && buf[ch] == '\033' && buf[ch+1] == '[' &&
1058 ch = grok_ansi (buf, ch+2, pa) + 1;
1060 while (cnt-ch >= 2 && buf[ch] == '\033' && buf[ch+1] == ']' &&
1061 check_attachment_marker ((char *) buf+ch) == 0)
1063 while (buf[ch++] != '\a')
1068 /* is anything left to do? */
1072 k = mbrtowc (&wc, (char *)buf+ch, cnt-ch, &mbstate);
1073 if (k == -2 || k == -1)
1075 dprint (1, (debugfile, "%s:%d: mbrtowc returned %d; errno = %d.\n",
1076 __FILE__, __LINE__, k, errno));
1077 if (col + 4 > wrap_cols)
1081 printw ("\\%03o", buf[ch]);
1088 /* Handle backspace */
1096 while ((wc1 = 0, mbstate1 = mbstate,
1097 k1 = k + mbrtowc (&wc1, (char *)buf+ch+k, cnt-ch-k, &mbstate1),
1098 k1 - k > 0 && wc1 == '\b') &&
1100 k2 = mbrtowc (&wc1, (char *)buf+ch+k1, cnt-ch-k1, &mbstate1),
1101 k2 > 0 && IsWPrint (wc1)))
1105 special |= (wc == '_' && special & A_UNDERLINE)
1106 ? A_UNDERLINE : A_BOLD;
1108 else if (wc == '_' || wc1 == '_')
1110 special |= A_UNDERLINE;
1111 wc = (wc1 == '_') ? wc : wc1;
1115 /* special = 0; / * overstrike: nothing to do! */
1125 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1126 special || last_special || pa->attr))
1128 resolve_color (*lineInfo, n, vch, flags, special, pa);
1129 last_special = special;
1137 if (col + t > wrap_cols)
1143 else if (wc == '\n')
1145 else if (wc == '\t')
1152 for (; col < t; col++)
1157 else if (wc < 0x20 || wc == 0x7f)
1159 if (col + 2 > wrap_cols)
1163 printw ("^%c", ('@' + wc) & 0x7f);
1165 else if (wc < 0x100)
1167 if (col + 4 > wrap_cols)
1171 printw ("\\%03o", wc);
1175 if (col + 1 > wrap_cols)
1179 addch (replacement_char ());
1185 *pspecial = special;
1191 * flags M_SHOWFLAT, show characters (used for displaying help)
1192 * M_SHOWCOLOR, show characters in color
1193 * otherwise don't show characters
1194 * M_HIDE, don't show quoted text
1195 * M_SEARCH, resolve search patterns
1196 * M_TYPES, compute line's type
1197 * M_PAGER_NSKIP, keeps leading whitespace
1198 * M_PAGER_MARKER, eventually show markers
1201 * -1 EOF was reached
1202 * 0 normal exit, line was not displayed
1203 * >0 normal exit, line was displayed
1207 display_line (FILE *f, long *last_pos, struct line_t **lineInfo, int n,
1208 int *last, int *max, int flags, struct q_class_t **QuoteList,
1209 int *q_level, int *force_redraw, regex_t *SearchRE)
1211 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1212 unsigned char *buf_ptr = buf;
1213 int ch, vch, col, cnt, b_read;
1214 int buf_ready = 0, change_last = 0;
1219 ansi_attr a = {0,0,0,-1};
1220 regmatch_t pmatch[1];
1230 safe_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1231 for (ch = *last; ch < *max ; ch++)
1233 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1234 (*lineInfo)[ch].type = -1;
1235 (*lineInfo)[ch].search_cnt = -1;
1236 (*lineInfo)[ch].syntax = safe_malloc (sizeof (struct syntax_t));
1237 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last = -1;
1241 /* only do color hiliting if we are viewing a message */
1242 if (flags & (M_SHOWCOLOR | M_TYPES))
1244 if ((*lineInfo)[n].type == -1)
1246 /* determine the line class */
1247 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1254 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1255 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1257 /* avoid race condition for continuation lines when scrolling up */
1258 for (m = n + 1; m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation; m++)
1259 (*lineInfo)[m].type = (*lineInfo)[n].type;
1262 /* this also prevents searching through the hidden lines */
1263 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1264 flags = 0; /* M_NOSHOW */
1267 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1268 * don't want to compute it every time M_TYPES is set, since this
1269 * would slow down the "bottom" function unacceptably. A compromise
1270 * solution is hence to call regexec() again, just to find out the
1271 * length of the quote prefix.
1273 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1274 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1276 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1282 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1283 (*lineInfo)[n].quote = classify_quote (QuoteList,
1284 (char *) fmt + pmatch[0].rm_so,
1285 pmatch[0].rm_eo - pmatch[0].rm_so,
1286 force_redraw, q_level);
1289 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation && (*lineInfo)[n].search_cnt == -1)
1291 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1299 (*lineInfo)[n].search_cnt = 0;
1300 while (regexec (SearchRE, (char *) fmt + offset, 1, pmatch, (offset ? REG_NOTBOL : 0)) == 0)
1302 if (++((*lineInfo)[n].search_cnt) > 1)
1303 safe_realloc (&((*lineInfo)[n].search),
1304 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1306 (*lineInfo)[n].search = safe_malloc (sizeof (struct syntax_t));
1307 pmatch[0].rm_so += offset;
1308 pmatch[0].rm_eo += offset;
1309 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first = pmatch[0].rm_so;
1310 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last = pmatch[0].rm_eo;
1312 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1313 offset++; /* avoid degenerate cases */
1315 offset = pmatch[0].rm_eo;
1321 if (!(flags & M_SHOW) && (*lineInfo)[n+1].offset > 0)
1323 /* we've already scanned this line, so just exit */
1326 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n+1].offset > 0)
1328 /* no need to try to display this line... */
1329 return (1); /* fake display */
1332 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1333 sizeof (buf), &buf_ready)) < 0)
1340 /* now chose a good place to break the line */
1341 cnt = format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col, &special);
1342 buf_ptr = buf + cnt;
1344 /* move the break point only if smart_wrap is set */
1345 if (option (OPTWRAP))
1349 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n' && buf[cnt] != '\r')
1352 /* skip trailing blanks */
1353 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1358 buf_ptr = buf + cnt; /* a very long word... */
1360 if (!(flags & M_PAGER_NSKIP))
1361 /* skip leading blanks on the next line too */
1362 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1366 if (*buf_ptr == '\r')
1368 if (*buf_ptr == '\n')
1371 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n+1].continuation)
1372 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1373 (*lineInfo)[n+1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1375 /* if we don't need to display the line we are done */
1376 if (!(flags & M_SHOW))
1379 /* display the line */
1380 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1382 /* avoid a bug in ncurses... */
1383 #ifndef USE_SLANG_CURSES
1386 SETCOLOR (MT_COLOR_NORMAL);
1391 /* end the last color pattern (needed by S-Lang) */
1392 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1393 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1396 * Fill the blank space at the end of the line with the prevailing color.
1397 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1398 * to make sure to reset the color *after* that
1400 if (flags & M_SHOWCOLOR)
1402 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1403 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1404 def_color = ((*lineInfo)[m].syntax)[0].color;
1406 def_color = ColorDefs[ (*lineInfo)[m].type ];
1408 attrset (def_color);
1410 bkgdset (def_color | ' ');
1414 /* ncurses always wraps lines when you get to the right side of the
1415 * screen, but S-Lang seems to only wrap if the next character is *not*
1418 #ifndef USE_SLANG_CURSES
1424 * reset the color back to normal. This *must* come after the
1425 * addch('\n'), otherwise the color for this line will not be
1426 * filled to the right margin.
1428 if (flags & M_SHOWCOLOR)
1430 SETCOLOR(MT_COLOR_NORMAL);
1431 BKGDSET(MT_COLOR_NORMAL);
1434 /* build a return code */
1435 if (!(flags & M_SHOW))
1442 upNLines (int nlines, struct line_t *info, int cur, int hiding)
1444 while (cur > 0 && nlines > 0)
1447 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1454 static struct mapping_t PagerHelp[] = {
1455 { N_("Exit"), OP_EXIT },
1456 { N_("PrevPg"), OP_PREV_PAGE },
1457 { N_("NextPg"), OP_NEXT_PAGE },
1460 static struct mapping_t PagerHelpExtra[] = {
1461 { N_("View Attachm."), OP_VIEW_ATTACHMENTS },
1462 { N_("Del"), OP_DELETE },
1463 { N_("Reply"), OP_REPLY },
1464 { N_("Next"), OP_MAIN_NEXT_UNDELETED },
1469 static struct mapping_t PagerNewsHelpExtra[] = {
1470 { N_("Post"), OP_POST },
1471 { N_("Followup"), OP_FOLLOWUP },
1472 { N_("Del"), OP_DELETE },
1473 { N_("Next"), OP_MAIN_NEXT_UNDELETED },
1480 /* This pager is actually not so simple as it once was. It now operates in
1481 two modes: one for viewing messages and the other for viewing help. These
1482 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1483 is there so that we can do operations on the current message without the
1484 need to pop back out to the main-menu. */
1486 mutt_pager (const char *banner, const char *fname, int flags, pager_t *extra)
1488 static char searchbuf[STRING];
1489 char buffer[LONG_STRING];
1490 char helpstr[SHORT_STRING*2];
1491 char tmphelp[SHORT_STRING*2];
1492 int maxLine, lastLine = 0;
1493 struct line_t *lineInfo;
1494 struct q_class_t *QuoteList = NULL;
1495 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1496 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1498 int redraw = REDRAW_FULL;
1500 long last_pos = 0, last_offset = 0;
1501 int old_smart_wrap, old_markers;
1504 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1505 int has_types = (IsHeader(extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1507 int bodyoffset = 1; /* offset of first line of real text */
1508 int statusoffset = 0; /* offset for the status bar */
1509 int helpoffset = LINES - 2; /* offset for the help bar. */
1510 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1512 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1513 int indexoffset = 0; /* offset for the PI */
1514 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1515 int indicator = indexlen / 3; /* the indicator line of the PI */
1516 int old_PagerIndexLines; /* some people want to resize it
1517 * while inside the pager... */
1523 if (!(flags & M_SHOWCOLOR))
1524 flags |= M_SHOWFLAT;
1526 if ((fp = fopen (fname, "r")) == NULL)
1528 mutt_perror (fname);
1532 if (stat (fname, &sb) != 0)
1534 mutt_perror (fname);
1540 /* Initialize variables */
1542 if (IsHeader (extra) && !extra->hdr->read)
1544 Context->msgnotreadyet = extra->hdr->msgno;
1545 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1548 lineInfo = safe_malloc (sizeof (struct line_t) * (maxLine = LINES));
1549 for (i = 0 ; i < maxLine ; i++)
1551 memset (&lineInfo[i], 0, sizeof (struct line_t));
1552 lineInfo[i].type = -1;
1553 lineInfo[i].search_cnt = -1;
1554 lineInfo[i].syntax = safe_malloc (sizeof (struct syntax_t));
1555 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1558 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1559 if (IsHeader (extra))
1561 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1562 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1564 (Context && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1567 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1571 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1572 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1573 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1584 if (redraw & REDRAW_FULL)
1586 SETCOLOR (MT_COLOR_NORMAL);
1587 /* clear() doesn't optimize screen redraws */
1591 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1592 indexlen = Context->vcount + 1;
1594 indexlen = PagerIndexLines;
1596 indicator = indexlen / 3;
1598 if (option (OPTSTATUSONTOP))
1601 statusoffset = IsHeader (extra) ? indexlen : 0;
1602 bodyoffset = statusoffset + 1;
1603 helpoffset = LINES - 2;
1604 bodylen = helpoffset - bodyoffset;
1605 if (!option (OPTHELP))
1612 statusoffset = LINES - 2;
1613 if (!option (OPTHELP))
1615 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1616 bodylen = statusoffset - bodyoffset;
1619 if (option (OPTHELP))
1621 SETCOLOR (MT_COLOR_STATUS);
1622 move (helpoffset, 0);
1623 mutt_paddstr (COLS, helpstr);
1624 SETCOLOR (MT_COLOR_NORMAL);
1627 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1630 if ((SearchCompiled = Resize->SearchCompiled))
1633 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1634 SearchFlag = M_SEARCH;
1635 SearchBack = Resize->SearchBack;
1637 lines = Resize->line;
1638 redraw |= REDRAW_SIGWINCH;
1644 if (IsHeader (extra) && PagerIndexLines)
1648 /* only allocate the space if/when we need the index.
1649 Initialise the menu as per the main index */
1650 index = mutt_new_menu();
1651 index->menu = MENU_MAIN;
1652 index->make_entry = index_make_entry;
1653 index->color = index_color;
1654 index->max = Context->vcount;
1655 index->current = extra->hdr->virtual;
1658 SETCOLOR (MT_COLOR_NORMAL);
1659 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1661 index->pagelen = indexlen - 1;
1663 /* some fudge to work out where abouts the indicator should go */
1664 if (index->current - indicator < 0)
1666 else if (index->max - index->current < index->pagelen - indicator)
1667 index->top = index->max - index->pagelen;
1669 index->top = index->current - indicator;
1671 menu_redraw_index(index);
1674 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1678 if (redraw & REDRAW_SIGWINCH)
1682 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1683 has_types | SearchFlag, &QuoteList, &q_level, &force_redraw,
1685 if (!lineInfo[i].continuation && ++j == lines)
1691 redraw |= REDRAW_SIDEBAR;
1695 if ((redraw & REDRAW_BODY) || topline != oldtopline)
1698 move (bodyoffset, SidebarWidth);
1699 curline = oldtopline = topline;
1703 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1)
1705 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1707 (flags & M_DISPLAYFLAGS) | hideQuoted | SearchFlag,
1708 &QuoteList, &q_level, &force_redraw, &SearchRE) > 0)
1711 move(lines + bodyoffset, SidebarWidth);
1712 redraw |= REDRAW_SIDEBAR;
1714 last_offset = lineInfo[curline].offset;
1715 } while (force_redraw);
1717 SETCOLOR (MT_COLOR_TILDE);
1718 BKGDSET (MT_COLOR_TILDE);
1719 while (lines < bodylen)
1722 if (option (OPTTILDE))
1726 move(lines + bodyoffset, SidebarWidth);
1728 /* We are going to update the pager status bar, so it isn't
1729 * necessary to reset to normal color now. */
1731 redraw |= REDRAW_STATUS; /* need to update the % seen */
1734 if (redraw & REDRAW_STATUS)
1736 /* print out the pager status bar */
1737 SETCOLOR (MT_COLOR_STATUS);
1738 BKGDSET (MT_COLOR_STATUS);
1739 CLEARLINE_WIN(statusoffset);
1740 if (IsHeader (extra))
1742 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1743 size_t l2 = sizeof (buffer);
1744 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1745 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1747 else if (IsMsgAttach (extra))
1749 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1750 size_t l2 = sizeof (buffer);
1751 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1752 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1754 mutt_paddstr (COLS-10-SidebarWidth, IsHeader (extra) || IsMsgAttach (extra) ?
1757 if (last_pos < sb.st_size - 1)
1758 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1760 addstr (topline == 0 ? "all)" : "end)");
1761 BKGDSET (MT_COLOR_NORMAL);
1762 SETCOLOR (MT_COLOR_NORMAL);
1765 if (redraw & REDRAW_SIDEBAR)
1766 draw_sidebar(MENU_PAGER);
1768 if ((redraw & REDRAW_INDEX) && index)
1770 /* redraw the pager_index indicator, because the
1771 * flags for this message might have changed. */
1772 menu_redraw_current (index);
1773 draw_sidebar(MENU_PAGER);
1774 /* print out the index status bar */
1775 menu_status_line (buffer, sizeof (buffer), index, NONULL(Status));
1776 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)),
1778 SETCOLOR (MT_COLOR_STATUS);
1779 mutt_paddstr (COLS-SidebarWidth, buffer);
1780 SETCOLOR (MT_COLOR_NORMAL);
1782 /* if we're not using the index, update every time */
1784 draw_sidebar(MENU_PAGER);
1788 move (statusoffset, COLS-1);
1790 ch = km_dokey (MENU_PAGER);
1792 mutt_clear_error ();
1800 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1803 mutt_resize_screen ();
1805 /* Store current position. */
1807 for (i = 0; i <= topline; i++)
1808 if (!lineInfo[i].continuation)
1811 if (flags & M_PAGER_RETWINCH)
1813 Resize = safe_malloc (sizeof (struct resize));
1815 Resize->line = lines;
1816 Resize->SearchCompiled = SearchCompiled;
1817 Resize->SearchBack = SearchBack;
1820 rc = OP_REFORMAT_WINCH;
1824 for (i = 0; i < maxLine; i++)
1826 lineInfo[i].offset = 0;
1827 lineInfo[i].type = -1;
1828 lineInfo[i].continuation = 0;
1829 lineInfo[i].chunks = 0;
1830 lineInfo[i].search_cnt = -1;
1831 lineInfo[i].quote = NULL;
1833 safe_realloc (&(lineInfo[i].syntax),
1834 sizeof (struct syntax_t));
1835 if (SearchCompiled && lineInfo[i].search)
1836 FREE (&(lineInfo[i].search));
1842 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1847 clearok(stdscr,TRUE);/*force complete redraw*/
1867 if (lineInfo[curline].offset < sb.st_size-1)
1869 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1871 else if (option (OPTPAGERSTOP))
1873 /* emulate "less -q" and don't go on to the next message. */
1874 mutt_error _("Bottom of message is shown.");
1878 /* end of the current message, so display the next message. */
1879 rc = OP_MAIN_NEXT_UNDELETED;
1887 topline = upNLines (bodylen-PagerContext, lineInfo, topline, hideQuoted);
1890 mutt_error _("Top of message is shown.");
1894 if (lineInfo[curline].offset < sb.st_size-1)
1899 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1905 mutt_error _("Bottom of message is shown.");
1910 topline = upNLines (1, lineInfo, topline, hideQuoted);
1912 mutt_error _("Top of message is shown.");
1919 mutt_error _("Top of message is shown.");
1924 topline = upNLines (bodylen/2, lineInfo, topline, hideQuoted);
1926 mutt_error _("Top of message is shown.");
1930 if (lineInfo[curline].offset < sb.st_size-1)
1932 topline = upNLines (bodylen/2, lineInfo, curline, hideQuoted);
1934 else if (option (OPTPAGERSTOP))
1936 /* emulate "less -q" and don't go on to the next message. */
1937 mutt_error _("Bottom of message is shown.");
1941 /* end of the current message, so display the next message. */
1942 rc = OP_MAIN_NEXT_UNDELETED;
1947 case OP_SEARCH_NEXT:
1948 case OP_SEARCH_OPPOSITE:
1952 if ((!SearchBack && ch==OP_SEARCH_NEXT) ||
1953 (SearchBack &&ch==OP_SEARCH_OPPOSITE))
1955 /* searching forward */
1956 for (i = topline + 1; i < lastLine; i++)
1958 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1959 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1966 mutt_error _("Not found.");
1970 /* searching backward */
1971 for (i = topline - 1; i >= 0; i--)
1973 if ((!hideQuoted || (has_types &&
1974 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1975 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1982 mutt_error _("Not found.");
1985 if (lineInfo[topline].search_cnt > 0)
1986 SearchFlag = M_SEARCH;
1990 /* no previous search pattern, so fall through to search */
1993 case OP_SEARCH_REVERSE:
1994 strfcpy (buffer, searchbuf, sizeof (buffer));
1995 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1996 _("Search: ")), buffer, sizeof (buffer),
2000 if (!strcmp (buffer, searchbuf))
2004 /* do an implicit search-next */
2005 if (ch == OP_SEARCH)
2006 ch = OP_SEARCH_NEXT;
2008 ch = OP_SEARCH_OPPOSITE;
2017 strfcpy (searchbuf, buffer, sizeof (searchbuf));
2019 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
2020 if (ch == OP_SEARCH)
2022 else if (ch == OP_SEARCH_REVERSE)
2027 regfree (&SearchRE);
2028 for (i = 0; i < lastLine; i++)
2030 if (lineInfo[i].search)
2031 FREE (&(lineInfo[i].search));
2032 lineInfo[i].search_cnt = -1;
2036 if ((err = REGCOMP (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf))) != 0)
2038 regerror (err, &SearchRE, buffer, sizeof (buffer));
2039 mutt_error ("%s", buffer);
2040 regfree (&SearchRE);
2041 for (i = 0; i < maxLine ; i++)
2044 if (lineInfo[i].search)
2045 FREE (&(lineInfo[i].search));
2046 lineInfo[i].search_cnt = -1;
2054 /* update the search pointers */
2056 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2057 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
2058 &QuoteList, &q_level,
2059 &force_redraw, &SearchRE) == 0) {
2061 redraw |= REDRAW_SIDEBAR;
2066 /* searching forward */
2067 for (i = topline; i < lastLine; i++)
2069 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2070 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2074 if (i < lastLine) topline = i;
2078 /* searching backward */
2079 for (i = topline; i >= 0; i--)
2081 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2082 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2086 if (i >= 0) topline = i;
2089 if (lineInfo[topline].search_cnt == 0)
2092 mutt_error _("Not found.");
2095 SearchFlag = M_SEARCH;
2097 redraw = REDRAW_BODY;
2100 case OP_SEARCH_TOGGLE:
2103 SearchFlag ^= M_SEARCH;
2104 redraw = REDRAW_BODY;
2109 /* don't let the user enter the help-menu from the help screen! */
2113 mutt_help (MENU_PAGER);
2114 redraw = REDRAW_FULL;
2118 mutt_error _("Help is currently being shown.");
2121 case OP_PAGER_HIDE_QUOTED:
2124 hideQuoted ^= M_HIDE;
2125 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
2126 topline = upNLines (1, lineInfo, topline, hideQuoted);
2128 redraw = REDRAW_BODY;
2132 case OP_PAGER_SKIP_QUOTED:
2136 int new_topline = topline;
2138 while ((new_topline < lastLine ||
2139 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2140 new_topline, &lastLine, &maxLine, M_TYPES,
2141 &QuoteList, &q_level, &force_redraw, &SearchRE))))
2142 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
2143 redraw |= REDRAW_SIDEBAR;
2149 mutt_error _("No more quoted text.");
2153 while ((new_topline < lastLine ||
2154 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2155 new_topline, &lastLine, &maxLine, M_TYPES,
2156 &QuoteList, &q_level, &force_redraw, &SearchRE))))
2157 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2159 redraw |= REDRAW_SIDEBAR;
2164 mutt_error _("No more unquoted text after quoted text.");
2167 topline = new_topline;
2171 case OP_PAGER_BOTTOM: /* move to the end of the file */
2172 if (lineInfo[curline].offset < sb.st_size - 1)
2175 /* make sure the types are defined to the end of file */
2176 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2177 &maxLine, has_types,
2178 &QuoteList, &q_level, &force_redraw,
2181 redraw |= REDRAW_SIDEBAR;
2183 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2186 mutt_error _("Bottom of message is shown.");
2190 clearok (stdscr, TRUE);
2191 redraw = REDRAW_FULL;
2195 km_error_key (MENU_PAGER);
2198 /* --------------------------------------------------------------------
2199 * The following are operations on the current message rather than
2200 * adjusting the view of the message.
2203 case OP_BOUNCE_MESSAGE:
2204 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra))
2206 if (IsMsgAttach (extra))
2207 mutt_attach_bounce (extra->fp, extra->hdr,
2208 extra->idx, extra->idxlen,
2211 ci_bounce_message (extra->hdr, &redraw);
2215 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra))
2217 if (IsMsgAttach (extra))
2218 mutt_attach_resend (extra->fp, extra->hdr,
2219 extra->idx, extra->idxlen,
2222 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2223 redraw = REDRAW_FULL;
2226 case OP_CHECK_TRADITIONAL:
2227 CHECK_MODE (IsHeader (extra));
2228 if (!(WithCrypto & APPLICATION_PGP))
2230 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED))
2233 rc = OP_CHECK_TRADITIONAL;
2237 case OP_CREATE_ALIAS:
2238 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2239 if (IsMsgAttach (extra))
2240 mutt_create_alias (extra->bdy->hdr->env, NULL);
2242 mutt_create_alias (extra->hdr->env, NULL);
2243 MAYBE_REDRAW (redraw);
2246 case OP_PURGE_MESSAGE:
2248 CHECK_MODE(IsHeader (extra));
2252 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2255 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2256 mutt_set_flag (Context, extra->hdr, M_PURGED,
2257 ch != OP_PURGE_MESSAGE ? 0 : 1);
2258 if (option (OPTDELETEUNTAG))
2259 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2260 redraw = REDRAW_STATUS | REDRAW_INDEX;
2261 if (option (OPTRESOLVE))
2264 rc = OP_MAIN_NEXT_UNDELETED;
2268 case OP_DELETE_THREAD:
2269 case OP_DELETE_SUBTHREAD:
2270 CHECK_MODE(IsHeader (extra));
2274 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2277 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2278 ch == OP_DELETE_THREAD ? 0 : 1);
2282 if (option (OPTDELETEUNTAG))
2283 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2284 ch == OP_DELETE_THREAD ? 0 : 1);
2285 if (option (OPTRESOLVE))
2287 rc = OP_MAIN_NEXT_UNDELETED;
2291 if (!option (OPTRESOLVE) && PagerIndexLines)
2292 redraw = REDRAW_FULL;
2294 redraw = REDRAW_STATUS | REDRAW_INDEX;
2298 case OP_DISPLAY_ADDRESS:
2299 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2300 if (IsMsgAttach (extra))
2301 mutt_display_address (extra->bdy->hdr->env);
2303 mutt_display_address (extra->hdr->env);
2306 case OP_ENTER_COMMAND:
2307 old_smart_wrap = option (OPTWRAP);
2308 old_markers = option (OPTMARKERS);
2309 old_PagerIndexLines = PagerIndexLines;
2311 CurrentMenu = MENU_PAGER;
2312 mutt_enter_command ();
2314 if (option (OPTNEEDRESORT))
2316 unset_option (OPTNEEDRESORT);
2317 CHECK_MODE(IsHeader (extra));
2318 set_option (OPTNEEDRESORT);
2321 if (old_PagerIndexLines != PagerIndexLines)
2324 mutt_menuDestroy (&index);
2328 if (option (OPTWRAP) != old_smart_wrap ||
2329 option (OPTMARKERS) != old_markers)
2331 if (flags & M_PAGER_RETWINCH)
2334 rc = OP_REFORMAT_WINCH;
2338 /* count the real lines above */
2340 for (i = 0; i <= topline; i++)
2342 if (!lineInfo[i].continuation)
2346 /* we need to restart the whole thing */
2347 for (i = 0; i < maxLine; i++)
2349 lineInfo[i].offset = 0;
2350 lineInfo[i].type = -1;
2351 lineInfo[i].continuation = 0;
2352 lineInfo[i].chunks = 0;
2353 lineInfo[i].search_cnt = -1;
2354 lineInfo[i].quote = NULL;
2356 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2357 if (SearchCompiled && lineInfo[i].search)
2358 FREE (&(lineInfo[i].search));
2363 regfree (&SearchRE);
2368 /* try to keep the old position */
2371 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2372 &lastLine, &maxLine,
2373 (has_types ? M_TYPES : 0),
2374 &QuoteList, &q_level, &force_redraw,
2377 redraw |= REDRAW_SIDEBAR;
2378 if (! lineInfo[topline].continuation)
2387 if (option (OPTFORCEREDRAWPAGER))
2388 redraw = REDRAW_FULL;
2389 unset_option (OPTFORCEREDRAWINDEX);
2390 unset_option (OPTFORCEREDRAWPAGER);
2393 case OP_FLAG_MESSAGE:
2394 CHECK_MODE(IsHeader (extra));
2398 if (Context->magic == M_POP)
2401 mutt_error _("Can't change 'important' flag on POP server.");
2407 CHECK_IMAP_ACL(IMAP_ACL_WRITE);
2411 if (Context->magic == M_NNTP)
2414 mutt_error _("Can't change 'important' flag on NNTP server.");
2419 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2420 redraw = REDRAW_STATUS | REDRAW_INDEX;
2421 if (option (OPTRESOLVE))
2424 rc = OP_MAIN_NEXT_UNDELETED;
2429 CHECK_MODE(IsHeader (extra) || IsAttach (extra));
2430 if (IsAttach (extra))
2431 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2433 mutt_pipe_message (extra->hdr);
2434 MAYBE_REDRAW (redraw);
2438 CHECK_MODE(IsHeader (extra) || IsAttach (extra));
2439 if (IsAttach (extra))
2440 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2442 mutt_print_message (extra->hdr);
2446 CHECK_MODE(IsHeader (extra) && !IsAttach (extra));
2448 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2449 redraw = REDRAW_FULL;
2454 CHECK_MODE(IsHeader (extra) && !IsAttach (extra));
2456 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2457 !((NNTP_DATA *)extra->ctx->data)->allowed &&
2458 query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
2460 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2461 redraw = REDRAW_FULL;
2464 case OP_FORWARD_TO_GROUP:
2465 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2467 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2468 !((NNTP_DATA *)extra->ctx->data)->allowed &&
2469 query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
2471 if (IsMsgAttach (extra))
2472 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2473 extra->idxlen, extra->bdy, SENDNEWS);
2475 ci_send_message (SENDNEWS|SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2476 redraw = REDRAW_FULL;
2480 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2483 if (IsMsgAttach (extra))
2484 followup_to = extra->bdy->hdr->env->followup_to;
2486 followup_to = extra->hdr->env->followup_to;
2488 if (!followup_to || mutt_strcasecmp (followup_to, "poster") ||
2489 query_quadoption (OPT_FOLLOWUPTOPOSTER,_("Reply by mail as poster prefers?")) != M_YES)
2491 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2492 !((NNTP_DATA *)extra->ctx->data)->allowed &&
2493 query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
2495 if (IsMsgAttach (extra))
2496 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2497 extra->idxlen, extra->bdy, SENDNEWS|SENDREPLY);
2499 ci_send_message (SENDNEWS|SENDREPLY, NULL, NULL,
2500 extra->ctx, extra->hdr);
2501 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,
2514 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2515 redraw = REDRAW_FULL;
2518 case OP_RECALL_MESSAGE:
2519 CHECK_MODE(IsHeader (extra) && !IsAttach(extra));
2521 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2522 redraw = REDRAW_FULL;
2525 case OP_GROUP_REPLY:
2526 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2528 if (IsMsgAttach (extra))
2529 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2530 extra->idxlen, extra->bdy, SENDREPLY|SENDGROUPREPLY);
2532 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx, extra->hdr);
2533 redraw = REDRAW_FULL;
2537 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2539 if (IsMsgAttach (extra))
2540 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2541 extra->idxlen, extra->bdy, SENDREPLY|SENDLISTREPLY);
2543 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx, extra->hdr);
2544 redraw = REDRAW_FULL;
2547 case OP_FORWARD_MESSAGE:
2548 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2550 if (IsMsgAttach (extra))
2551 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2552 extra->idxlen, extra->bdy, 0);
2554 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2555 redraw = REDRAW_FULL;
2558 case OP_DECRYPT_SAVE:
2566 if (IsAttach (extra))
2568 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr, NULL);
2572 case OP_COPY_MESSAGE:
2573 case OP_DECODE_SAVE:
2574 case OP_DECODE_COPY:
2575 case OP_DECRYPT_COPY:
2576 if (!WithCrypto && ch == OP_DECRYPT_COPY)
2581 CHECK_MODE(IsHeader (extra));
2582 if (mutt_save_message (extra->hdr,
2583 (ch == OP_DECRYPT_SAVE) ||
2584 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2585 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2586 (ch == OP_DECRYPT_SAVE) || (ch == OP_DECRYPT_COPY) ||
2588 &redraw) == 0 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2589 || ch == OP_DECRYPT_SAVE
2592 if (option (OPTRESOLVE))
2595 rc = OP_MAIN_NEXT_UNDELETED;
2598 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2600 MAYBE_REDRAW (redraw);
2603 case OP_SHELL_ESCAPE:
2604 mutt_shell_escape ();
2605 MAYBE_REDRAW (redraw);
2609 CHECK_MODE(IsHeader (extra));
2610 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2612 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2613 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2614 ? NULL : Context->last_tag);
2616 redraw = REDRAW_STATUS | REDRAW_INDEX;
2617 if (option (OPTRESOLVE))
2625 CHECK_MODE(IsHeader (extra));
2629 CHECK_IMAP_ACL(IMAP_ACL_SEEN);
2632 if (extra->hdr->read || extra->hdr->old)
2633 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2635 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2637 Context->msgnotreadyet = -1;
2638 redraw = REDRAW_STATUS | REDRAW_INDEX;
2639 if (option (OPTRESOLVE))
2642 rc = OP_MAIN_NEXT_UNDELETED;
2647 CHECK_MODE(IsHeader (extra));
2651 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2654 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2655 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2656 redraw = REDRAW_STATUS | REDRAW_INDEX;
2657 if (option (OPTRESOLVE))
2664 case OP_UNDELETE_THREAD:
2665 case OP_UNDELETE_SUBTHREAD:
2666 CHECK_MODE(IsHeader (extra));
2670 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2673 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2674 ch == OP_UNDELETE_THREAD ? 0 : 1)
2675 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2676 ch == OP_UNDELETE_THREAD ? 0 : 1);
2680 if (option (OPTRESOLVE))
2682 rc = (ch == OP_DELETE_THREAD) ?
2683 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2687 if (!option (OPTRESOLVE) && PagerIndexLines)
2688 redraw = REDRAW_FULL;
2690 redraw = REDRAW_STATUS | REDRAW_INDEX;
2702 case OP_VIEW_ATTACHMENTS:
2703 if (flags & M_PAGER_ATTACHMENT)
2706 rc = OP_ATTACH_COLLAPSE;
2709 CHECK_MODE(IsHeader (extra));
2710 mutt_view_attachments (extra->hdr);
2711 if (extra->hdr->attach_del)
2712 Context->changed = 1;
2713 redraw = REDRAW_FULL;
2718 if (!(WithCrypto & APPLICATION_PGP))
2723 CHECK_MODE(IsHeader(extra));
2725 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2726 redraw = REDRAW_FULL;
2730 case OP_FORGET_PASSPHRASE:
2731 crypt_forget_passphrase ();
2734 case OP_EXTRACT_KEYS:
2740 CHECK_MODE(IsHeader(extra));
2741 crypt_extract_keys_from_messages(extra->hdr);
2742 redraw = REDRAW_FULL;
2745 case OP_SIDEBAR_SCROLL_UP:
2746 case OP_SIDEBAR_SCROLL_DOWN:
2747 case OP_SIDEBAR_NEXT:
2748 case OP_SIDEBAR_PREV:
2749 scroll_sidebar(ch, MENU_PAGER);
2758 if (IsHeader (extra))
2759 Context->msgnotreadyet = -1;
2761 cleanup_quote (&QuoteList);
2763 for (i = 0; i < maxLine ; i++)
2765 FREE (&(lineInfo[i].syntax));
2766 if (SearchCompiled && lineInfo[i].search)
2767 FREE (&(lineInfo[i].search));
2771 regfree (&SearchRE);
2776 mutt_menuDestroy(&index);
2777 return (rc != -1 ? rc : 0);