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"
20 #include "mutt_regex.h"
22 #include "mutt_menu.h"
33 #include "imap_private.h"
36 #include "mutt_crypt.h"
45 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
47 #define IsAttach(x) (x && (x)->bdy)
48 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
49 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
50 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
51 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
53 static const char *Not_available_in_this_menu =
54 N_("Not available in this menu.");
55 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
56 static const char *Function_not_permitted_in_attach_message_mode =
57 N_("Function not permitted in attach-message mode.");
59 #define CHECK_MODE(x) if (!(x)) \
62 mutt_error _(Not_available_in_this_menu); \
66 #define CHECK_READONLY if (Context->readonly) \
69 mutt_error _(Mailbox_is_read_only); \
73 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
76 mutt_error _(Function_not_permitted_in_attach_message_mode); \
81 /* the error message returned here could be better. */
82 #define CHECK_IMAP_ACL(aclbit) if (Context->magic == M_IMAP) \
83 if (mutt_bit_isset (((IMAP_DATA *)Context->data)->capabilities, ACL) \
84 && !mutt_bit_isset(((IMAP_DATA *)Context->data)->rights,aclbit)){ \
86 mutt_error ("Operation not permitted by the IMAP ACL for this mailbox"); \
96 struct q_class_t *next, *prev;
97 struct q_class_t *down, *up;
112 struct syntax_t *syntax;
113 struct syntax_t *search;
114 struct q_class_t *quote;
117 #define ANSI_OFF (1<<0)
118 #define ANSI_BLINK (1<<1)
119 #define ANSI_BOLD (1<<2)
120 #define ANSI_UNDERLINE (1<<3)
121 #define ANSI_REVERSE (1<<4)
122 #define ANSI_COLOR (1<<5)
124 typedef struct _ansi_attr {
131 static short InHelp = 0;
133 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
134 static struct resize {
141 #define NumSigLines 4
143 static int check_sig (const char *s, struct line_t *info, int n)
147 while (n > 0 && count <= NumSigLines) {
148 if (info[n].type != MT_COLOR_SIGNATURE)
157 if (count > NumSigLines) {
158 /* check for a blank line */
172 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
173 int special, ansi_attr * a)
175 int def_color; /* color without syntax hilight */
176 int color; /* final color */
177 static int last_color; /* last color set */
178 int search = 0, i, m;
181 last_color = -1; /* force attrset() */
183 if (lineInfo[n].continuation) {
184 if (!cnt && option (OPTMARKERS)) {
185 SETCOLOR (MT_COLOR_MARKERS);
187 last_color = ColorDefs[MT_COLOR_MARKERS];
189 m = (lineInfo[n].syntax)[0].first;
190 cnt += (lineInfo[n].syntax)[0].last;
194 if (!(flags & M_SHOWCOLOR))
195 def_color = ColorDefs[MT_COLOR_NORMAL];
196 else if (lineInfo[m].type == MT_COLOR_HEADER)
197 def_color = (lineInfo[m].syntax)[0].color;
199 def_color = ColorDefs[lineInfo[m].type];
201 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
202 struct q_class_t *class = lineInfo[m].quote;
205 def_color = class->color;
207 while (class && class->length > cnt) {
208 def_color = class->color;
215 if (flags & M_SHOWCOLOR) {
216 for (i = 0; i < lineInfo[m].chunks; i++) {
217 /* we assume the chunks are sorted */
218 if (cnt > (lineInfo[m].syntax)[i].last)
220 if (cnt < (lineInfo[m].syntax)[i].first)
222 if (cnt != (lineInfo[m].syntax)[i].last) {
223 color = (lineInfo[m].syntax)[i].color;
226 /* don't break here, as cnt might be
227 * in the next chunk as well */
231 if (flags & M_SEARCH) {
232 for (i = 0; i < lineInfo[m].search_cnt; i++) {
233 if (cnt > (lineInfo[m].search)[i].last)
235 if (cnt < (lineInfo[m].search)[i].first)
237 if (cnt != (lineInfo[m].search)[i].last) {
238 color = ColorDefs[MT_COLOR_SEARCH];
245 /* handle "special" bold & underlined characters */
246 if (special || a->attr) {
248 if ((a->attr & ANSI_COLOR)) {
250 a->pair = mutt_alloc_color (a->fg, a->bg);
252 if (a->attr & ANSI_BOLD)
257 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
258 if (ColorDefs[MT_COLOR_BOLD] && !search)
259 color = ColorDefs[MT_COLOR_BOLD];
263 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
264 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
265 color = ColorDefs[MT_COLOR_UNDERLINE];
267 color ^= A_UNDERLINE;
269 else if (a->attr & ANSI_REVERSE) {
272 else if (a->attr & ANSI_BLINK) {
275 else if (a->attr & ANSI_OFF) {
280 if (color != last_color) {
286 static void append_line (struct line_t *lineInfo, int n, int cnt)
290 lineInfo[n + 1].type = lineInfo[n].type;
291 (lineInfo[n + 1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
292 lineInfo[n + 1].continuation = 1;
294 /* find the real start of the line */
295 for (m = n; m >= 0; m--)
296 if (lineInfo[m].continuation == 0)
299 (lineInfo[n + 1].syntax)[0].first = m;
300 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
301 cnt + (lineInfo[n].syntax)[0].last : cnt;
304 static void new_class_color (struct q_class_t *class, int *q_level)
306 class->index = (*q_level)++;
307 class->color = ColorQuote[class->index % ColorQuoteUsed];
311 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
312 int index, int *q_level)
314 struct q_class_t *q_list;
317 new_class->index = -1;
320 if (q_list->index >= index) {
322 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
325 q_list = q_list->down;
326 else if (q_list->next)
327 q_list = q_list->next;
329 while (!q_list->next) {
335 q_list = q_list->next;
339 new_class->index = index;
340 new_class->color = ColorQuote[index % ColorQuoteUsed];
344 static void cleanup_quote (struct q_class_t **QuoteList)
346 struct q_class_t *ptr;
349 if ((*QuoteList)->down)
350 cleanup_quote (&((*QuoteList)->down));
351 ptr = (*QuoteList)->next;
352 if ((*QuoteList)->prefix)
353 FREE (&(*QuoteList)->prefix);
361 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
362 const char *qptr, int length,
363 int *force_redraw, int *q_level)
365 struct q_class_t *q_list = *QuoteList;
366 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
368 int offset, tail_lng;
371 if (ColorQuoteUsed <= 1) {
372 /* not much point in classifying quotes... */
374 if (*QuoteList == NULL) {
375 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
376 class->color = ColorQuote[0];
382 /* Did I mention how much I like emulating Lisp in C? */
384 /* classify quoting prefix */
386 if (length <= q_list->length) {
387 /* case 1: check the top level nodes */
389 if (mutt_strncmp (qptr, q_list->prefix, length) == 0) {
390 if (length == q_list->length)
391 return q_list; /* same prefix: return the current class */
393 /* found shorter prefix */
395 /* add a node above q_list */
397 (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
398 tmp->prefix = (char *) safe_calloc (1, length + 1);
399 strncpy (tmp->prefix, qptr, length);
400 tmp->length = length;
402 /* replace q_list by tmp in the top level list */
404 tmp->next = q_list->next;
405 q_list->next->prev = tmp;
408 tmp->prev = q_list->prev;
409 q_list->prev->next = tmp;
412 /* make q_list a child of tmp */
416 /* q_list has no siblings for now */
420 /* update the root if necessary */
421 if (q_list == *QuoteList)
424 index = q_list->index;
426 /* tmp should be the return class too */
429 /* next class to test; if tmp is a shorter prefix for another
430 * node, that node can only be in the top level list, so don't
431 * go down after this point
436 /* found another branch for which tmp is a shorter prefix */
438 /* save the next sibling for later */
441 /* unlink q_list from the top level list */
443 q_list->next->prev = q_list->prev;
445 q_list->prev->next = q_list->next;
447 /* at this point, we have a tmp->down; link q_list to it */
449 /* sibling order is important here, q_list should be linked last */
457 index = q_list->index;
459 /* next class to test; as above, we shouldn't go down */
463 /* we found a shorter prefix, so certain quotes have changed classes */
468 /* shorter, but not a substring of the current class: try next */
469 q_list = q_list->next;
474 /* case 2: try subclassing the current top level node */
476 /* tmp != NULL means we already found a shorter prefix at case 1 */
478 && mutt_strncmp (qptr, q_list->prefix, q_list->length) == 0) {
479 /* ok, it's a subclass somewhere on this branch */
482 offset = q_list->length;
484 q_list = q_list->down;
485 tail_lng = length - offset;
486 tail_qptr = (char *) qptr + offset;
489 if (length <= q_list->length) {
490 if (mutt_strncmp (tail_qptr, (q_list->prefix) + offset, tail_lng)
492 /* same prefix: return the current class */
493 if (length == q_list->length)
496 /* found shorter common prefix */
498 /* add a node above q_list */
499 tmp = (struct q_class_t *) safe_calloc (1,
502 tmp->prefix = (char *) safe_calloc (1, length + 1);
503 strncpy (tmp->prefix, qptr, length);
504 tmp->length = length;
506 /* replace q_list by tmp */
508 tmp->next = q_list->next;
509 q_list->next->prev = tmp;
512 tmp->prev = q_list->prev;
513 q_list->prev->next = tmp;
516 /* make q_list a child of tmp */
518 tmp->up = q_list->up;
520 if (tmp->up->down == q_list)
523 /* q_list has no siblings */
527 index = q_list->index;
529 /* tmp should be the return class too */
532 /* next class to test */
536 /* found another branch for which tmp is a shorter prefix */
538 /* save the next sibling for later */
541 /* unlink q_list from the top level list */
543 q_list->next->prev = q_list->prev;
545 q_list->prev->next = q_list->next;
547 /* at this point, we have a tmp->down; link q_list to it */
556 index = q_list->index;
558 /* next class to test */
562 /* we found a shorter prefix, so we need a redraw */
567 q_list = q_list->next;
572 /* longer than the current prefix: try subclassing it */
574 && mutt_strncmp (tail_qptr, (q_list->prefix) + offset,
575 q_list->length - offset) == 0) {
576 /* still a subclass: go down one level */
578 offset = q_list->length;
580 q_list = q_list->down;
581 tail_lng = length - offset;
582 tail_qptr = (char *) qptr + offset;
587 /* nope, try the next prefix */
588 q_list = q_list->next;
594 /* still not found so far: add it as a sibling to the current node */
597 (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
598 tmp->prefix = (char *) safe_calloc (1, length + 1);
599 strncpy (tmp->prefix, qptr, length);
600 tmp->length = length;
603 tmp->next = ptr->down;
604 ptr->down->prev = tmp;
609 new_class_color (tmp, q_level);
615 shift_class_colors (*QuoteList, tmp, index, q_level);
621 /* nope, try the next prefix */
622 q_list = q_list->next;
629 /* not found so far: add it as a top level class */
630 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
631 class->prefix = (char *) safe_calloc (1, length + 1);
632 strncpy (class->prefix, qptr, length);
633 class->length = length;
634 new_class_color (class, q_level);
637 class->next = *QuoteList;
638 (*QuoteList)->prev = class;
644 shift_class_colors (*QuoteList, tmp, index, q_level);
649 static int check_attachment_marker (char *);
652 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
653 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
656 COLOR_LINE *color_line;
657 regmatch_t pmatch[1], smatch[1];
658 int found, offset, null_rx, i;
660 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
662 lineInfo[n].type = MT_COLOR_NORMAL;
663 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
664 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
665 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
668 lineInfo[n].type = MT_COLOR_HDEFAULT;
669 color_line = ColorHdrList;
671 if (REGEXEC (color_line->rx, buf) == 0) {
672 lineInfo[n].type = MT_COLOR_HEADER;
673 lineInfo[n].syntax[0].color = color_line->pair;
676 color_line = color_line->next;
680 else if (mutt_strncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
681 lineInfo[n].type = MT_COLOR_NORMAL;
683 else if (mutt_strncmp ("[-- ", buf, 4) == 0)
684 lineInfo[n].type = MT_COLOR_ATTACHMENT;
686 else if (check_attachment_marker ((char *) raw) == 0)
687 lineInfo[n].type = MT_COLOR_ATTACHMENT;
689 else if (mutt_strcmp ("-- \n", buf) == 0
690 || mutt_strcmp ("-- \r\n", buf) == 0) {
693 lineInfo[n].type = MT_COLOR_SIGNATURE;
694 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
695 (lineInfo[i].type == MT_COLOR_NORMAL ||
696 lineInfo[i].type == MT_COLOR_QUOTED ||
697 lineInfo[i].type == MT_COLOR_HEADER)) {
699 if (lineInfo[i].chunks) {
700 lineInfo[i].chunks = 0;
701 safe_realloc (&(lineInfo[n].syntax), sizeof (struct syntax_t));
703 lineInfo[i++].type = MT_COLOR_SIGNATURE;
706 else if (check_sig (buf, lineInfo, n - 1) == 0)
707 lineInfo[n].type = MT_COLOR_SIGNATURE;
708 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
709 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
710 if (smatch[0].rm_so > 0) {
713 /* hack to avoid making an extra copy of buf */
714 c = buf[smatch[0].rm_so];
715 buf[smatch[0].rm_so] = 0;
717 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
718 if (q_classify && lineInfo[n].quote == NULL)
719 lineInfo[n].quote = classify_quote (QuoteList,
720 buf + pmatch[0].rm_so,
722 pmatch[0].rm_so, force_redraw,
724 lineInfo[n].type = MT_COLOR_QUOTED;
727 lineInfo[n].type = MT_COLOR_NORMAL;
729 buf[smatch[0].rm_so] = c;
732 lineInfo[n].type = MT_COLOR_NORMAL;
735 if (q_classify && lineInfo[n].quote == NULL)
736 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
737 pmatch[0].rm_eo - pmatch[0].rm_so,
738 force_redraw, q_level);
739 lineInfo[n].type = MT_COLOR_QUOTED;
743 lineInfo[n].type = MT_COLOR_NORMAL;
746 if (lineInfo[n].type == MT_COLOR_NORMAL ||
747 lineInfo[n].type == MT_COLOR_QUOTED) {
751 lineInfo[n].chunks = 0;
758 color_line = ColorBodyList;
760 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
761 (offset ? REG_NOTBOL : 0)) == 0) {
762 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
764 if (++(lineInfo[n].chunks) > 1)
765 safe_realloc (&(lineInfo[n].syntax),
766 (lineInfo[n].chunks) *
767 sizeof (struct syntax_t));
769 i = lineInfo[n].chunks - 1;
770 pmatch[0].rm_so += offset;
771 pmatch[0].rm_eo += offset;
773 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
774 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
775 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
776 (lineInfo[n].syntax)[i].color = color_line->pair;
777 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
778 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
784 null_rx = 1; /* empty regexp; don't add it, but keep looking */
786 color_line = color_line->next;
790 offset++; /* avoid degenerate cases */
792 offset = (lineInfo[n].syntax)[i].last;
793 } while (found || null_rx);
797 static int is_ansi (unsigned char *buf)
799 while (*buf && (isdigit (*buf) || *buf == ';'))
801 return (*buf == 'm');
804 static int check_attachment_marker (char *p)
806 char *q = AttachmentMarker;
808 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
809 return (int) (*p - *q);
812 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
816 while (isdigit (buf[x]) || buf[x] == ';')
819 /* Character Attributes */
820 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
824 mutt_free_color (a->fg, a->bg);
830 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
831 a->attr |= ANSI_BOLD;
834 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
835 a->attr |= ANSI_UNDERLINE;
838 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
839 a->attr |= ANSI_BLINK;
842 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
843 a->attr |= ANSI_REVERSE;
846 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
849 mutt_free_color (a->fg, a->bg);
855 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
858 mutt_free_color (a->fg, a->bg);
861 a->attr |= ANSI_COLOR;
862 a->fg = buf[pos + 1] - '0';
865 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
868 mutt_free_color (a->fg, a->bg);
871 a->attr |= ANSI_COLOR;
872 a->bg = buf[pos + 1] - '0';
876 while (pos < x && buf[pos] != ';')
887 fill_buffer (FILE * f, long *last_pos, long offset, unsigned char *buf,
888 unsigned char *fmt, size_t blen, int *buf_ready)
891 static int b_read = 0;
893 if (*buf_ready == 0) {
895 if (offset != *last_pos)
896 fseek (f, offset, 0);
897 if (fgets ((char *) buf, blen - 1, f) == NULL) {
901 *last_pos = ftell (f);
902 b_read = (int) (*last_pos - offset);
905 /* copy "buf" to "fmt", but without bold and underline controls */
908 if (*p == '\010' && (p > buf)) {
909 if (*(p + 1) == '_') /* underline */
911 else if (*(p + 1)) { /* bold or overstrike */
912 *(fmt - 1) = *(p + 1);
918 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
919 while (*p++ != 'm') /* skip ANSI sequence */
922 else if (*p == '\033' && *(p + 1) == ']'
923 && check_attachment_marker ((char *) p) == 0) {
924 dprint (2, (debugfile, "fill_buffer: Seen attachment marker.\n"));
925 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
942 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
943 int flags, ansi_attr * pa, int cnt,
944 int *pspace, int *pvch, int *pcol, int *pspecial)
946 int space = -1; /* index of the last space or TAB */
947 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
948 int ch, vch, k, last_special = -1, special = 0, t;
952 int wrap_cols = COLS;
954 if (!(flags & (M_SHOWFLAT)))
955 wrap_cols -= WrapMargin;
956 wrap_cols -= SidebarWidth;
961 /* FIXME: this should come from lineInfo */
962 memset (&mbstate, 0, sizeof (mbstate));
964 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
965 /* Handle ANSI sequences */
966 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
967 is_ansi (buf + ch + 2))
968 ch = grok_ansi (buf, ch + 2, pa) + 1;
970 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
971 check_attachment_marker ((char *) buf + ch) == 0) {
972 while (buf[ch++] != '\a')
977 /* is anything left to do? */
981 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
982 if (k == -2 || k == -1) {
983 dprint (1, (debugfile, "%s:%d: mbrtowc returned %d; errno = %d.\n",
984 __FILE__, __LINE__, k, errno));
985 if (col + 4 > wrap_cols)
989 printw ("\\%03o", buf[ch]);
996 /* Handle backspace */
1003 while ((wc1 = 0, mbstate1 = mbstate,
1005 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
1006 &mbstate1), k1 - k > 0 && wc1 == '\b')
1008 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1009 &mbstate1), k2 > 0 && IsWPrint (wc1))) {
1011 special |= (wc == '_' && special & A_UNDERLINE)
1012 ? A_UNDERLINE : A_BOLD;
1014 else if (wc == '_' || wc1 == '_') {
1015 special |= A_UNDERLINE;
1016 wc = (wc1 == '_') ? wc : wc1;
1019 /* special = 0; / * overstrike: nothing to do! */
1029 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1030 special || last_special || pa->attr)) {
1031 resolve_color (*lineInfo, n, vch, flags, special, pa);
1032 last_special = special;
1035 if (IsWPrint (wc)) {
1039 if (col + t > wrap_cols)
1045 else if (wc == '\n')
1047 else if (wc == '\t') {
1053 for (; col < t; col++)
1058 else if (wc < 0x20 || wc == 0x7f) {
1059 if (col + 2 > wrap_cols)
1063 printw ("^%c", ('@' + wc) & 0x7f);
1065 else if (wc < 0x100) {
1066 if (col + 4 > wrap_cols)
1070 printw ("\\%03o", wc);
1073 if (col + 1 > wrap_cols)
1077 addch (replacement_char ());
1083 *pspecial = special;
1089 * flags M_SHOWFLAT, show characters (used for displaying help)
1090 * M_SHOWCOLOR, show characters in color
1091 * otherwise don't show characters
1092 * M_HIDE, don't show quoted text
1093 * M_SEARCH, resolve search patterns
1094 * M_TYPES, compute line's type
1095 * M_PAGER_NSKIP, keeps leading whitespace
1096 * M_PAGER_MARKER, eventually show markers
1099 * -1 EOF was reached
1100 * 0 normal exit, line was not displayed
1101 * >0 normal exit, line was displayed
1105 display_line (FILE * f, long *last_pos, struct line_t **lineInfo, int n,
1106 int *last, int *max, int flags, struct q_class_t **QuoteList,
1107 int *q_level, int *force_redraw, regex_t * SearchRE)
1109 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1110 unsigned char *buf_ptr = buf;
1111 int ch, vch, col, cnt, b_read;
1112 int buf_ready = 0, change_last = 0;
1117 ansi_attr a = { 0, 0, 0, -1 };
1118 regmatch_t pmatch[1];
1125 if (*last == *max) {
1126 safe_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1127 for (ch = *last; ch < *max; ch++) {
1128 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1129 (*lineInfo)[ch].type = -1;
1130 (*lineInfo)[ch].search_cnt = -1;
1131 (*lineInfo)[ch].syntax = safe_malloc (sizeof (struct syntax_t));
1132 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1137 /* only do color hiliting if we are viewing a message */
1138 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1139 if ((*lineInfo)[n].type == -1) {
1140 /* determine the line class */
1142 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1149 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1150 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1152 /* avoid race condition for continuation lines when scrolling up */
1154 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1156 (*lineInfo)[m].type = (*lineInfo)[n].type;
1159 /* this also prevents searching through the hidden lines */
1160 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1161 flags = 0; /* M_NOSHOW */
1164 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1165 * don't want to compute it every time M_TYPES is set, since this
1166 * would slow down the "bottom" function unacceptably. A compromise
1167 * solution is hence to call regexec() again, just to find out the
1168 * length of the quote prefix.
1170 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1171 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1174 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1180 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1181 (*lineInfo)[n].quote = classify_quote (QuoteList,
1182 (char *) fmt + pmatch[0].rm_so,
1183 pmatch[0].rm_eo - pmatch[0].rm_so,
1184 force_redraw, q_level);
1187 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1188 && (*lineInfo)[n].search_cnt == -1) {
1190 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1198 (*lineInfo)[n].search_cnt = 0;
1200 (SearchRE, (char *) fmt + offset, 1, pmatch,
1201 (offset ? REG_NOTBOL : 0)) == 0) {
1202 if (++((*lineInfo)[n].search_cnt) > 1)
1203 safe_realloc (&((*lineInfo)[n].search),
1204 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1206 (*lineInfo)[n].search = safe_malloc (sizeof (struct syntax_t));
1207 pmatch[0].rm_so += offset;
1208 pmatch[0].rm_eo += offset;
1209 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1211 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1214 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1215 offset++; /* avoid degenerate cases */
1217 offset = pmatch[0].rm_eo;
1223 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1224 /* we've already scanned this line, so just exit */
1227 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1228 /* no need to try to display this line... */
1229 return (1); /* fake display */
1232 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1233 sizeof (buf), &buf_ready)) < 0) {
1239 /* now chose a good place to break the line */
1241 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1243 buf_ptr = buf + cnt;
1245 /* move the break point only if smart_wrap is set */
1246 if (option (OPTWRAP)) {
1248 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1249 && buf[cnt] != '\r') {
1251 /* skip trailing blanks */
1252 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1257 buf_ptr = buf + cnt; /* a very long word... */
1259 if (!(flags & M_PAGER_NSKIP))
1260 /* skip leading blanks on the next line too */
1261 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1265 if (*buf_ptr == '\r')
1267 if (*buf_ptr == '\n')
1270 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1271 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1272 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1274 /* if we don't need to display the line we are done */
1275 if (!(flags & M_SHOW))
1278 /* display the line */
1279 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1281 /* avoid a bug in ncurses... */
1282 #ifndef USE_SLANG_CURSES
1284 SETCOLOR (MT_COLOR_NORMAL);
1289 /* end the last color pattern (needed by S-Lang) */
1290 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1291 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1294 * Fill the blank space at the end of the line with the prevailing color.
1295 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1296 * to make sure to reset the color *after* that
1298 if (flags & M_SHOWCOLOR) {
1299 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1300 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1301 def_color = ((*lineInfo)[m].syntax)[0].color;
1303 def_color = ColorDefs[(*lineInfo)[m].type];
1305 attrset (def_color);
1307 bkgdset (def_color | ' ');
1311 /* ncurses always wraps lines when you get to the right side of the
1312 * screen, but S-Lang seems to only wrap if the next character is *not*
1315 #ifndef USE_SLANG_CURSES
1321 * reset the color back to normal. This *must* come after the
1322 * addch('\n'), otherwise the color for this line will not be
1323 * filled to the right margin.
1325 if (flags & M_SHOWCOLOR) {
1326 SETCOLOR (MT_COLOR_NORMAL);
1327 BKGDSET (MT_COLOR_NORMAL);
1330 /* build a return code */
1331 if (!(flags & M_SHOW))
1337 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1339 while (cur > 0 && nlines > 0) {
1341 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1348 static struct mapping_t PagerHelp[] = {
1349 {N_("Exit"), OP_EXIT},
1350 {N_("PrevPg"), OP_PREV_PAGE},
1351 {N_("NextPg"), OP_NEXT_PAGE},
1354 static struct mapping_t PagerHelpExtra[] = {
1355 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1356 {N_("Del"), OP_DELETE},
1357 {N_("Reply"), OP_REPLY},
1358 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1363 static struct mapping_t PagerNewsHelpExtra[] = {
1364 {N_("Post"), OP_POST},
1365 {N_("Followup"), OP_FOLLOWUP},
1366 {N_("Del"), OP_DELETE},
1367 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1374 /* This pager is actually not so simple as it once was. It now operates in
1375 two modes: one for viewing messages and the other for viewing help. These
1376 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1377 is there so that we can do operations on the current message without the
1378 need to pop back out to the main-menu. */
1380 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1382 static char searchbuf[STRING];
1383 char buffer[LONG_STRING];
1384 char helpstr[SHORT_STRING * 2];
1385 char tmphelp[SHORT_STRING * 2];
1386 int maxLine, lastLine = 0;
1387 struct line_t *lineInfo;
1388 struct q_class_t *QuoteList = NULL;
1389 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1390 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1392 int redraw = REDRAW_FULL;
1394 long last_pos = 0, last_offset = 0;
1395 int old_smart_wrap, old_markers;
1398 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1399 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1401 int bodyoffset = 1; /* offset of first line of real text */
1402 int statusoffset = 0; /* offset for the status bar */
1403 int helpoffset = LINES - 2; /* offset for the help bar. */
1404 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1406 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1407 int indexoffset = 0; /* offset for the PI */
1408 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1409 int indicator = indexlen / 3; /* the indicator line of the PI */
1410 int old_PagerIndexLines; /* some people want to resize it
1411 * while inside the pager... */
1417 if (!(flags & M_SHOWCOLOR))
1418 flags |= M_SHOWFLAT;
1420 if ((fp = fopen (fname, "r")) == NULL) {
1421 mutt_perror (fname);
1425 if (stat (fname, &sb) != 0) {
1426 mutt_perror (fname);
1432 /* Initialize variables */
1434 if (IsHeader (extra) && !extra->hdr->read) {
1435 Context->msgnotreadyet = extra->hdr->msgno;
1436 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1439 lineInfo = safe_malloc (sizeof (struct line_t) * (maxLine = LINES));
1440 for (i = 0; i < maxLine; i++) {
1441 memset (&lineInfo[i], 0, sizeof (struct line_t));
1442 lineInfo[i].type = -1;
1443 lineInfo[i].search_cnt = -1;
1444 lineInfo[i].syntax = safe_malloc (sizeof (struct syntax_t));
1445 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1448 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1449 if (IsHeader (extra)) {
1450 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1451 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1454 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1457 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1460 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1461 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1462 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1472 if (redraw & REDRAW_FULL) {
1473 SETCOLOR (MT_COLOR_NORMAL);
1474 /* clear() doesn't optimize screen redraws */
1478 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1479 indexlen = Context->vcount + 1;
1481 indexlen = PagerIndexLines;
1483 indicator = indexlen / 3;
1485 if (option (OPTSTATUSONTOP)) {
1487 statusoffset = IsHeader (extra) ? indexlen : 0;
1488 bodyoffset = statusoffset + 1;
1489 helpoffset = LINES - 2;
1490 bodylen = helpoffset - bodyoffset;
1491 if (!option (OPTHELP))
1497 statusoffset = LINES - 2;
1498 if (!option (OPTHELP))
1500 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1501 bodylen = statusoffset - bodyoffset;
1504 if (option (OPTHELP)) {
1505 SETCOLOR (MT_COLOR_STATUS);
1506 move (helpoffset, 0);
1507 mutt_paddstr (COLS, helpstr);
1508 SETCOLOR (MT_COLOR_NORMAL);
1511 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1512 if (Resize != NULL) {
1513 if ((SearchCompiled = Resize->SearchCompiled)) {
1515 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1516 SearchFlag = M_SEARCH;
1517 SearchBack = Resize->SearchBack;
1519 lines = Resize->line;
1520 redraw |= REDRAW_SIGWINCH;
1526 if (IsHeader (extra) && PagerIndexLines) {
1527 if (index == NULL) {
1528 /* only allocate the space if/when we need the index.
1529 Initialise the menu as per the main index */
1530 index = mutt_new_menu ();
1531 index->menu = MENU_MAIN;
1532 index->make_entry = index_make_entry;
1533 index->color = index_color;
1534 index->max = Context->vcount;
1535 index->current = extra->hdr->virtual;
1538 SETCOLOR (MT_COLOR_NORMAL);
1539 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1541 index->pagelen = indexlen - 1;
1543 /* some fudge to work out where abouts the indicator should go */
1544 if (index->current - indicator < 0)
1546 else if (index->max - index->current < index->pagelen - indicator)
1547 index->top = index->max - index->pagelen;
1549 index->top = index->current - indicator;
1551 menu_redraw_index (index);
1554 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1558 if (redraw & REDRAW_SIGWINCH) {
1561 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1562 has_types | SearchFlag, &QuoteList, &q_level,
1563 &force_redraw, &SearchRE) == 0) {
1564 if (!lineInfo[i].continuation && ++j == lines) {
1569 redraw |= REDRAW_SIDEBAR;
1573 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1575 move (bodyoffset, SidebarWidth);
1576 curline = oldtopline = topline;
1580 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1581 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1583 (flags & M_DISPLAYFLAGS) | hideQuoted |
1584 SearchFlag, &QuoteList, &q_level, &force_redraw,
1588 move (lines + bodyoffset, SidebarWidth);
1589 redraw |= REDRAW_SIDEBAR;
1591 last_offset = lineInfo[curline].offset;
1592 } while (force_redraw);
1594 SETCOLOR (MT_COLOR_TILDE);
1595 BKGDSET (MT_COLOR_TILDE);
1596 while (lines < bodylen) {
1598 if (option (OPTTILDE))
1602 move (lines + bodyoffset, SidebarWidth);
1604 /* We are going to update the pager status bar, so it isn't
1605 * necessary to reset to normal color now. */
1607 redraw |= REDRAW_STATUS; /* need to update the % seen */
1610 if (redraw & REDRAW_STATUS) {
1611 /* print out the pager status bar */
1612 SETCOLOR (MT_COLOR_STATUS);
1613 BKGDSET (MT_COLOR_STATUS);
1614 CLEARLINE_WIN (statusoffset);
1615 if (IsHeader (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->hdr, M_FORMAT_MAKEPRINT);
1622 else if (IsMsgAttach (extra)) {
1623 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1624 size_t l2 = sizeof (buffer);
1626 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1627 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1629 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)),
1630 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);
1631 mutt_paddstr (COLS - 10, IsHeader (extra)
1632 || IsMsgAttach (extra) ? buffer : banner);
1635 if (last_pos < sb.st_size - 1)
1636 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1638 addstr (topline == 0 ? "all)" : "end)");
1639 BKGDSET (MT_COLOR_NORMAL);
1640 SETCOLOR (MT_COLOR_NORMAL);
1643 if (redraw & REDRAW_SIDEBAR)
1644 draw_sidebar (MENU_PAGER);
1646 if ((redraw & REDRAW_INDEX) && index) {
1647 /* redraw the pager_index indicator, because the
1648 * flags for this message might have changed. */
1649 menu_redraw_current (index);
1650 draw_sidebar (MENU_PAGER);
1651 /* print out the index status bar */
1652 menu_status_line (buffer, sizeof (buffer), index, NONULL (Status));
1653 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)),
1654 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);
1655 SETCOLOR (MT_COLOR_STATUS);
1656 mutt_paddstr (COLS - (option (OPTSTATUSONTOP) ? 0 : SidebarWidth),
1658 SETCOLOR (MT_COLOR_NORMAL);
1660 /* if we're not using the index, update every time */
1662 draw_sidebar (MENU_PAGER);
1666 move (statusoffset, COLS - 1);
1668 ch = km_dokey (MENU_PAGER);
1670 mutt_clear_error ();
1677 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1678 else if (SigWinch) {
1679 mutt_resize_screen ();
1681 /* Store current position. */
1683 for (i = 0; i <= topline; i++)
1684 if (!lineInfo[i].continuation)
1687 if (flags & M_PAGER_RETWINCH) {
1688 Resize = safe_malloc (sizeof (struct resize));
1690 Resize->line = lines;
1691 Resize->SearchCompiled = SearchCompiled;
1692 Resize->SearchBack = SearchBack;
1695 rc = OP_REFORMAT_WINCH;
1698 for (i = 0; i < maxLine; i++) {
1699 lineInfo[i].offset = 0;
1700 lineInfo[i].type = -1;
1701 lineInfo[i].continuation = 0;
1702 lineInfo[i].chunks = 0;
1703 lineInfo[i].search_cnt = -1;
1704 lineInfo[i].quote = NULL;
1706 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
1707 if (SearchCompiled && lineInfo[i].search)
1708 FREE (&(lineInfo[i].search));
1714 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1719 clearok (stdscr, TRUE); /*force complete redraw */
1723 else if (ch == -1) {
1737 if (lineInfo[curline].offset < sb.st_size - 1) {
1738 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1740 else if (option (OPTPAGERSTOP)) {
1741 /* emulate "less -q" and don't go on to the next message. */
1742 mutt_error _("Bottom of message is shown.");
1745 /* end of the current message, so display the next message. */
1746 rc = OP_MAIN_NEXT_UNDELETED;
1754 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1757 mutt_error _("Top of message is shown.");
1761 if (lineInfo[curline].offset < sb.st_size - 1) {
1764 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1770 mutt_error _("Bottom of message is shown.");
1775 topline = upNLines (1, lineInfo, topline, hideQuoted);
1777 mutt_error _("Top of message is shown.");
1784 mutt_error _("Top of message is shown.");
1789 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1791 mutt_error _("Top of message is shown.");
1795 if (lineInfo[curline].offset < sb.st_size - 1) {
1796 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1798 else if (option (OPTPAGERSTOP)) {
1799 /* emulate "less -q" and don't go on to the next message. */
1800 mutt_error _("Bottom of message is shown.");
1803 /* end of the current message, so display the next message. */
1804 rc = OP_MAIN_NEXT_UNDELETED;
1809 case OP_SEARCH_NEXT:
1810 case OP_SEARCH_OPPOSITE:
1811 if (SearchCompiled) {
1813 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1814 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1815 /* searching forward */
1816 for (i = topline + 1; i < lastLine; i++) {
1817 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1818 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1825 mutt_error _("Not found.");
1828 /* searching backward */
1829 for (i = topline - 1; i >= 0; i--) {
1830 if ((!hideQuoted || (has_types &&
1831 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1832 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1839 mutt_error _("Not found.");
1842 if (lineInfo[topline].search_cnt > 0)
1843 SearchFlag = M_SEARCH;
1847 /* no previous search pattern, so fall through to search */
1850 case OP_SEARCH_REVERSE:
1851 strfcpy (buffer, searchbuf, sizeof (buffer));
1852 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1853 _("Search: ")), buffer, sizeof (buffer),
1857 if (!strcmp (buffer, searchbuf)) {
1858 if (SearchCompiled) {
1859 /* do an implicit search-next */
1860 if (ch == OP_SEARCH)
1861 ch = OP_SEARCH_NEXT;
1863 ch = OP_SEARCH_OPPOSITE;
1872 strfcpy (searchbuf, buffer, sizeof (searchbuf));
1874 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1875 if (ch == OP_SEARCH)
1877 else if (ch == OP_SEARCH_REVERSE)
1880 if (SearchCompiled) {
1881 regfree (&SearchRE);
1882 for (i = 0; i < lastLine; i++) {
1883 if (lineInfo[i].search)
1884 FREE (&(lineInfo[i].search));
1885 lineInfo[i].search_cnt = -1;
1890 REGCOMP (&SearchRE, searchbuf,
1891 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1892 regerror (err, &SearchRE, buffer, sizeof (buffer));
1893 mutt_error ("%s", buffer);
1894 regfree (&SearchRE);
1895 for (i = 0; i < maxLine; i++) {
1897 if (lineInfo[i].search)
1898 FREE (&(lineInfo[i].search));
1899 lineInfo[i].search_cnt = -1;
1906 /* update the search pointers */
1908 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1909 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1910 &QuoteList, &q_level,
1911 &force_redraw, &SearchRE) == 0) {
1913 redraw |= REDRAW_SIDEBAR;
1917 /* searching forward */
1918 for (i = topline; i < lastLine; i++) {
1919 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1920 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1928 /* searching backward */
1929 for (i = topline; i >= 0; i--) {
1930 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1931 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1939 if (lineInfo[topline].search_cnt == 0) {
1941 mutt_error _("Not found.");
1944 SearchFlag = M_SEARCH;
1946 redraw = REDRAW_BODY;
1949 case OP_SEARCH_TOGGLE:
1950 if (SearchCompiled) {
1951 SearchFlag ^= M_SEARCH;
1952 redraw = REDRAW_BODY;
1957 /* don't let the user enter the help-menu from the help screen! */
1960 mutt_help (MENU_PAGER);
1961 redraw = REDRAW_FULL;
1965 mutt_error _("Help is currently being shown.");
1968 case OP_PAGER_HIDE_QUOTED:
1970 hideQuoted ^= M_HIDE;
1971 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1972 topline = upNLines (1, lineInfo, topline, hideQuoted);
1974 redraw = REDRAW_BODY;
1978 case OP_PAGER_SKIP_QUOTED:
1981 int new_topline = topline;
1983 while ((new_topline < lastLine ||
1984 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1985 new_topline, &lastLine,
1986 &maxLine, M_TYPES, &QuoteList,
1987 &q_level, &force_redraw,
1989 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
1990 redraw |= REDRAW_SIDEBAR;
1995 mutt_error _("No more quoted text.");
2000 while ((new_topline < lastLine ||
2001 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2002 new_topline, &lastLine,
2003 &maxLine, M_TYPES, &QuoteList,
2004 &q_level, &force_redraw,
2006 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2008 redraw |= REDRAW_SIDEBAR;
2012 mutt_error _("No more unquoted text after quoted text.");
2016 topline = new_topline;
2020 case OP_PAGER_BOTTOM: /* move to the end of the file */
2021 if (lineInfo[curline].offset < sb.st_size - 1) {
2023 /* make sure the types are defined to the end of file */
2024 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2025 &maxLine, has_types,
2026 &QuoteList, &q_level, &force_redraw,
2029 redraw |= REDRAW_SIDEBAR;
2031 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2034 mutt_error _("Bottom of message is shown.");
2038 clearok (stdscr, TRUE);
2039 redraw = REDRAW_FULL;
2043 km_error_key (MENU_PAGER);
2046 /* --------------------------------------------------------------------
2047 * The following are operations on the current message rather than
2048 * adjusting the view of the message.
2051 case OP_BOUNCE_MESSAGE:
2052 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2054 if (IsMsgAttach (extra))
2055 mutt_attach_bounce (extra->fp, extra->hdr,
2056 extra->idx, extra->idxlen, extra->bdy);
2058 ci_bounce_message (extra->hdr, &redraw);
2062 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2064 if (IsMsgAttach (extra))
2065 mutt_attach_resend (extra->fp, extra->hdr,
2066 extra->idx, extra->idxlen, extra->bdy);
2068 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2069 redraw = REDRAW_FULL;
2072 case OP_CHECK_TRADITIONAL:
2073 CHECK_MODE (IsHeader (extra));
2074 if (!(WithCrypto & APPLICATION_PGP))
2076 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2078 rc = OP_CHECK_TRADITIONAL;
2082 case OP_CREATE_ALIAS:
2083 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2084 if (IsMsgAttach (extra))
2085 mutt_create_alias (extra->bdy->hdr->env, NULL);
2087 mutt_create_alias (extra->hdr->env, NULL);
2088 MAYBE_REDRAW (redraw);
2091 case OP_PURGE_MESSAGE:
2093 CHECK_MODE (IsHeader (extra));
2097 CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2100 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2101 mutt_set_flag (Context, extra->hdr, M_PURGED,
2102 ch != OP_PURGE_MESSAGE ? 0 : 1);
2103 if (option (OPTDELETEUNTAG))
2104 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2105 redraw = REDRAW_STATUS | REDRAW_INDEX;
2106 if (option (OPTRESOLVE)) {
2108 rc = OP_MAIN_NEXT_UNDELETED;
2112 case OP_DELETE_THREAD:
2113 case OP_DELETE_SUBTHREAD:
2114 CHECK_MODE (IsHeader (extra));
2118 CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2121 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2122 ch == OP_DELETE_THREAD ? 0 : 1);
2125 if (option (OPTDELETEUNTAG))
2126 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2127 ch == OP_DELETE_THREAD ? 0 : 1);
2128 if (option (OPTRESOLVE)) {
2129 rc = OP_MAIN_NEXT_UNDELETED;
2133 if (!option (OPTRESOLVE) && PagerIndexLines)
2134 redraw = REDRAW_FULL;
2136 redraw = REDRAW_STATUS | REDRAW_INDEX;
2140 case OP_DISPLAY_ADDRESS:
2141 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2142 if (IsMsgAttach (extra))
2143 mutt_display_address (extra->bdy->hdr->env);
2145 mutt_display_address (extra->hdr->env);
2148 case OP_ENTER_COMMAND:
2149 old_smart_wrap = option (OPTWRAP);
2150 old_markers = option (OPTMARKERS);
2151 old_PagerIndexLines = PagerIndexLines;
2153 CurrentMenu = MENU_PAGER;
2154 mutt_enter_command ();
2156 if (option (OPTNEEDRESORT)) {
2157 unset_option (OPTNEEDRESORT);
2158 CHECK_MODE (IsHeader (extra));
2159 set_option (OPTNEEDRESORT);
2162 if (old_PagerIndexLines != PagerIndexLines) {
2164 mutt_menuDestroy (&index);
2168 if (option (OPTWRAP) != old_smart_wrap ||
2169 option (OPTMARKERS) != old_markers) {
2170 if (flags & M_PAGER_RETWINCH) {
2172 rc = OP_REFORMAT_WINCH;
2176 /* count the real lines above */
2178 for (i = 0; i <= topline; i++) {
2179 if (!lineInfo[i].continuation)
2183 /* we need to restart the whole thing */
2184 for (i = 0; i < maxLine; i++) {
2185 lineInfo[i].offset = 0;
2186 lineInfo[i].type = -1;
2187 lineInfo[i].continuation = 0;
2188 lineInfo[i].chunks = 0;
2189 lineInfo[i].search_cnt = -1;
2190 lineInfo[i].quote = NULL;
2192 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2193 if (SearchCompiled && lineInfo[i].search)
2194 FREE (&(lineInfo[i].search));
2197 if (SearchCompiled) {
2198 regfree (&SearchRE);
2203 /* try to keep the old position */
2206 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2207 &lastLine, &maxLine,
2208 (has_types ? M_TYPES : 0),
2209 &QuoteList, &q_level, &force_redraw,
2211 redraw |= REDRAW_SIDEBAR;
2212 if (!lineInfo[topline].continuation)
2221 if (option (OPTFORCEREDRAWPAGER))
2222 redraw = REDRAW_FULL;
2223 unset_option (OPTFORCEREDRAWINDEX);
2224 unset_option (OPTFORCEREDRAWPAGER);
2227 case OP_FLAG_MESSAGE:
2228 CHECK_MODE (IsHeader (extra));
2232 if (Context->magic == M_POP) {
2234 mutt_error _("Can't change 'important' flag on POP server.");
2241 CHECK_IMAP_ACL (IMAP_ACL_WRITE);
2245 if (Context->magic == M_NNTP) {
2247 mutt_error _("Can't change 'important' flag on NNTP server.");
2253 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2254 redraw = REDRAW_STATUS | REDRAW_INDEX;
2255 if (option (OPTRESOLVE)) {
2257 rc = OP_MAIN_NEXT_UNDELETED;
2262 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2263 if (IsAttach (extra))
2264 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2266 mutt_pipe_message (extra->hdr);
2267 MAYBE_REDRAW (redraw);
2271 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2272 if (IsAttach (extra))
2273 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2275 mutt_print_message (extra->hdr);
2279 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2281 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2282 redraw = REDRAW_FULL;
2287 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2289 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2290 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2291 query_quadoption (OPT_TOMODERATED,
2293 ("Posting to this group not allowed, may be moderated. Continue?"))
2296 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2297 redraw = REDRAW_FULL;
2300 case OP_FORWARD_TO_GROUP:
2301 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
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_forward (extra->fp, extra->hdr, extra->idx,
2312 extra->idxlen, extra->bdy, SENDNEWS);
2314 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2316 redraw = REDRAW_FULL;
2320 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2323 if (IsMsgAttach (extra))
2324 followup_to = extra->bdy->hdr->env->followup_to;
2326 followup_to = extra->hdr->env->followup_to;
2328 if (!followup_to || mutt_strcasecmp (followup_to, "poster") ||
2329 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2330 _("Reply by mail as poster prefers?")) != M_YES) {
2331 if (extra->ctx && extra->ctx->magic == M_NNTP
2332 && !((NNTP_DATA *) extra->ctx->data)->allowed
2333 && query_quadoption (OPT_TOMODERATED,
2335 ("Posting to this group not allowed, may be moderated. Continue?"))
2338 if (IsMsgAttach (extra))
2339 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2340 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2342 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2343 extra->ctx, extra->hdr);
2344 redraw = REDRAW_FULL;
2350 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2352 if (IsMsgAttach (extra))
2353 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2354 extra->idxlen, extra->bdy, SENDREPLY);
2356 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2357 redraw = REDRAW_FULL;
2360 case OP_RECALL_MESSAGE:
2361 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2363 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2364 redraw = REDRAW_FULL;
2367 case OP_GROUP_REPLY:
2368 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2370 if (IsMsgAttach (extra))
2371 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2372 extra->idxlen, extra->bdy,
2373 SENDREPLY | SENDGROUPREPLY);
2375 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2377 redraw = REDRAW_FULL;
2381 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2383 if (IsMsgAttach (extra))
2384 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2385 extra->idxlen, extra->bdy,
2386 SENDREPLY | SENDLISTREPLY);
2388 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2390 redraw = REDRAW_FULL;
2393 case OP_FORWARD_MESSAGE:
2394 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2396 if (IsMsgAttach (extra))
2397 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2398 extra->idxlen, extra->bdy, 0);
2400 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2401 redraw = REDRAW_FULL;
2404 case OP_DECRYPT_SAVE:
2411 if (IsAttach (extra)) {
2412 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2417 case OP_COPY_MESSAGE:
2418 case OP_DECODE_SAVE:
2419 case OP_DECODE_COPY:
2420 case OP_DECRYPT_COPY:
2421 if (!WithCrypto && ch == OP_DECRYPT_COPY) {
2425 CHECK_MODE (IsHeader (extra));
2426 if (mutt_save_message (extra->hdr,
2427 (ch == OP_DECRYPT_SAVE) ||
2428 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2429 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2430 (ch == OP_DECRYPT_SAVE)
2431 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2432 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2433 || ch == OP_DECRYPT_SAVE)) {
2434 if (option (OPTRESOLVE)) {
2436 rc = OP_MAIN_NEXT_UNDELETED;
2439 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2441 MAYBE_REDRAW (redraw);
2444 case OP_SHELL_ESCAPE:
2445 mutt_shell_escape ();
2446 MAYBE_REDRAW (redraw);
2450 CHECK_MODE (IsHeader (extra));
2451 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2453 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2454 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2455 ? NULL : Context->last_tag);
2457 redraw = REDRAW_STATUS | REDRAW_INDEX;
2458 if (option (OPTRESOLVE)) {
2465 CHECK_MODE (IsHeader (extra));
2469 CHECK_IMAP_ACL (IMAP_ACL_SEEN);
2472 if (extra->hdr->read || extra->hdr->old)
2473 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2475 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2477 Context->msgnotreadyet = -1;
2478 redraw = REDRAW_STATUS | REDRAW_INDEX;
2479 if (option (OPTRESOLVE)) {
2481 rc = OP_MAIN_NEXT_UNDELETED;
2486 CHECK_MODE (IsHeader (extra));
2490 CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2493 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2494 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2495 redraw = REDRAW_STATUS | REDRAW_INDEX;
2496 if (option (OPTRESOLVE)) {
2502 case OP_UNDELETE_THREAD:
2503 case OP_UNDELETE_SUBTHREAD:
2504 CHECK_MODE (IsHeader (extra));
2508 CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2511 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2512 ch == OP_UNDELETE_THREAD ? 0 : 1)
2513 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2514 ch == OP_UNDELETE_THREAD ? 0 : 1);
2517 if (option (OPTRESOLVE)) {
2518 rc = (ch == OP_DELETE_THREAD) ?
2519 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2523 if (!option (OPTRESOLVE) && PagerIndexLines)
2524 redraw = REDRAW_FULL;
2526 redraw = REDRAW_STATUS | REDRAW_INDEX;
2536 redraw |= REDRAW_SIDEBAR;
2539 case OP_VIEW_ATTACHMENTS:
2540 if (flags & M_PAGER_ATTACHMENT) {
2542 rc = OP_ATTACH_COLLAPSE;
2545 CHECK_MODE (IsHeader (extra));
2546 mutt_view_attachments (extra->hdr);
2547 if (extra->hdr->attach_del)
2548 Context->changed = 1;
2549 redraw = REDRAW_FULL;
2554 if (!(WithCrypto & APPLICATION_PGP)) {
2558 CHECK_MODE (IsHeader (extra));
2560 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2561 redraw = REDRAW_FULL;
2565 case OP_FORGET_PASSPHRASE:
2566 crypt_forget_passphrase ();
2569 case OP_EXTRACT_KEYS:
2574 CHECK_MODE (IsHeader (extra));
2575 crypt_extract_keys_from_messages (extra->hdr);
2576 redraw = REDRAW_FULL;
2579 case OP_SIDEBAR_SCROLL_UP:
2580 case OP_SIDEBAR_SCROLL_DOWN:
2581 case OP_SIDEBAR_NEXT:
2582 case OP_SIDEBAR_NEXT_NEW:
2583 case OP_SIDEBAR_PREV:
2584 case OP_SIDEBAR_PREV_NEW:
2585 scroll_sidebar (ch, MENU_PAGER);
2594 if (IsHeader (extra))
2595 Context->msgnotreadyet = -1;
2597 cleanup_quote (&QuoteList);
2599 for (i = 0; i < maxLine; i++) {
2600 FREE (&(lineInfo[i].syntax));
2601 if (SearchCompiled && lineInfo[i].search)
2602 FREE (&(lineInfo[i].search));
2604 if (SearchCompiled) {
2605 regfree (&SearchRE);
2610 mutt_menuDestroy (&index);
2611 return (rc != -1 ? rc : 0);