2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
5 * Parts were written/modified by:
6 * Nico Golde <nico@ngolde.de>
7 * Andreas Krennmair <ak@synflood.at>
9 * This file is part of mutt-ng, see http://www.muttng.org/.
10 * It's licensed under the GNU General Public License,
11 * please see the file GPL in the top level source directory.
19 #include "mutt_curses.h"
21 #include "mutt_menu.h"
33 #include "imap_private.h"
36 #include "mutt_crypt.h"
42 #include "lib/debug.h"
51 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
53 #define IsAttach(x) (x && (x)->bdy)
54 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
55 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
56 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
57 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
59 static const char *Not_available_in_this_menu =
60 N_("Not available in this menu.");
61 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
62 static const char *Function_not_permitted_in_attach_message_mode =
63 N_("Function not permitted in attach-message mode.");
65 #define CHECK_MODE(x) if (!(x)) \
68 mutt_error _(Not_available_in_this_menu); \
72 #define CHECK_READONLY if (Context->readonly) \
75 mutt_error _(Mailbox_is_read_only); \
79 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
82 mutt_error _(Function_not_permitted_in_attach_message_mode); \
91 struct q_class_t *next, *prev;
92 struct q_class_t *down, *up;
107 struct syntax_t *syntax;
108 struct syntax_t *search;
109 struct q_class_t *quote;
112 #define ANSI_OFF (1<<0)
113 #define ANSI_BLINK (1<<1)
114 #define ANSI_BOLD (1<<2)
115 #define ANSI_UNDERLINE (1<<3)
116 #define ANSI_REVERSE (1<<4)
117 #define ANSI_COLOR (1<<5)
119 typedef struct _ansi_attr {
126 static short InHelp = 0;
128 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
129 static struct resize {
136 #define NumSigLines 4
138 static int check_sig (const char *s, struct line_t *info, int n)
142 while (n > 0 && count <= NumSigLines) {
143 if (info[n].type != MT_COLOR_SIGNATURE)
152 if (count > NumSigLines) {
153 /* check for a blank line */
167 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
168 int special, ansi_attr * a)
170 int def_color; /* color without syntax hilight */
171 int color; /* final color */
172 static int last_color; /* last color set */
173 int search = 0, i, m;
176 last_color = -1; /* force attrset() */
178 if (lineInfo[n].continuation) {
179 if (!cnt && option (OPTMARKERS)) {
180 SETCOLOR (MT_COLOR_MARKERS);
182 last_color = ColorDefs[MT_COLOR_MARKERS];
184 m = (lineInfo[n].syntax)[0].first;
185 cnt += (lineInfo[n].syntax)[0].last;
189 if (!(flags & M_SHOWCOLOR))
190 def_color = ColorDefs[MT_COLOR_NORMAL];
191 else if (lineInfo[m].type == MT_COLOR_HEADER)
192 def_color = (lineInfo[m].syntax)[0].color;
194 def_color = ColorDefs[lineInfo[m].type];
196 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
197 struct q_class_t *class = lineInfo[m].quote;
200 def_color = class->color;
202 while (class && class->length > cnt) {
203 def_color = class->color;
210 if (flags & M_SHOWCOLOR) {
211 for (i = 0; i < lineInfo[m].chunks; i++) {
212 /* we assume the chunks are sorted */
213 if (cnt > (lineInfo[m].syntax)[i].last)
215 if (cnt < (lineInfo[m].syntax)[i].first)
217 if (cnt != (lineInfo[m].syntax)[i].last) {
218 color = (lineInfo[m].syntax)[i].color;
221 /* don't break here, as cnt might be
222 * in the next chunk as well */
226 if (flags & M_SEARCH) {
227 for (i = 0; i < lineInfo[m].search_cnt; i++) {
228 if (cnt > (lineInfo[m].search)[i].last)
230 if (cnt < (lineInfo[m].search)[i].first)
232 if (cnt != (lineInfo[m].search)[i].last) {
233 color = ColorDefs[MT_COLOR_SEARCH];
240 /* handle "special" bold & underlined characters */
241 if (special || a->attr) {
243 if ((a->attr & ANSI_COLOR)) {
245 a->pair = mutt_alloc_color (a->fg, a->bg);
247 if (a->attr & ANSI_BOLD)
252 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
253 if (ColorDefs[MT_COLOR_BOLD] && !search)
254 color = ColorDefs[MT_COLOR_BOLD];
258 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
259 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
260 color = ColorDefs[MT_COLOR_UNDERLINE];
262 color ^= A_UNDERLINE;
264 else if (a->attr & ANSI_REVERSE) {
267 else if (a->attr & ANSI_BLINK) {
270 else if (a->attr & ANSI_OFF) {
275 if (color != last_color) {
281 static void append_line (struct line_t *lineInfo, int n, int cnt)
285 lineInfo[n + 1].type = lineInfo[n].type;
286 (lineInfo[n + 1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
287 lineInfo[n + 1].continuation = 1;
289 /* find the real start of the line */
290 for (m = n; m >= 0; m--)
291 if (lineInfo[m].continuation == 0)
294 (lineInfo[n + 1].syntax)[0].first = m;
295 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
296 cnt + (lineInfo[n].syntax)[0].last : cnt;
299 static void new_class_color (struct q_class_t *class, int *q_level)
301 class->index = (*q_level)++;
302 class->color = ColorQuote[class->index % ColorQuoteUsed];
306 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
307 int index, int *q_level)
309 struct q_class_t *q_list;
312 new_class->index = -1;
315 if (q_list->index >= index) {
317 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
320 q_list = q_list->down;
321 else if (q_list->next)
322 q_list = q_list->next;
324 while (!q_list->next) {
330 q_list = q_list->next;
334 new_class->index = index;
335 new_class->color = ColorQuote[index % ColorQuoteUsed];
339 static void cleanup_quote (struct q_class_t **QuoteList)
341 struct q_class_t *ptr;
344 if ((*QuoteList)->down)
345 cleanup_quote (&((*QuoteList)->down));
346 ptr = (*QuoteList)->next;
347 if ((*QuoteList)->prefix)
348 FREE (&(*QuoteList)->prefix);
356 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
357 const char *qptr, int length,
358 int *force_redraw, int *q_level)
360 struct q_class_t *q_list = *QuoteList;
361 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
363 int offset, tail_lng;
366 if (ColorQuoteUsed <= 1) {
367 /* not much point in classifying quotes... */
369 if (*QuoteList == NULL) {
370 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
371 class->color = ColorQuote[0];
377 /* Did I mention how much I like emulating Lisp in C? */
379 /* classify quoting prefix */
381 if (length <= q_list->length) {
382 /* case 1: check the top level nodes */
384 if (safe_strncmp (qptr, q_list->prefix, length) == 0) {
385 if (length == q_list->length)
386 return q_list; /* same prefix: return the current class */
388 /* found shorter prefix */
390 /* add a node above q_list */
392 (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
393 tmp->prefix = (char *) safe_calloc (1, length + 1);
394 strncpy (tmp->prefix, qptr, length);
395 tmp->length = length;
397 /* replace q_list by tmp in the top level list */
399 tmp->next = q_list->next;
400 q_list->next->prev = tmp;
403 tmp->prev = q_list->prev;
404 q_list->prev->next = tmp;
407 /* make q_list a child of tmp */
411 /* q_list has no siblings for now */
415 /* update the root if necessary */
416 if (q_list == *QuoteList)
419 index = q_list->index;
421 /* tmp should be the return class too */
424 /* next class to test; if tmp is a shorter prefix for another
425 * node, that node can only be in the top level list, so don't
426 * go down after this point
431 /* found another branch for which tmp is a shorter prefix */
433 /* save the next sibling for later */
436 /* unlink q_list from the top level list */
438 q_list->next->prev = q_list->prev;
440 q_list->prev->next = q_list->next;
442 /* at this point, we have a tmp->down; link q_list to it */
444 /* sibling order is important here, q_list should be linked last */
452 index = q_list->index;
454 /* next class to test; as above, we shouldn't go down */
458 /* we found a shorter prefix, so certain quotes have changed classes */
463 /* shorter, but not a substring of the current class: try next */
464 q_list = q_list->next;
469 /* case 2: try subclassing the current top level node */
471 /* tmp != NULL means we already found a shorter prefix at case 1 */
473 && safe_strncmp (qptr, q_list->prefix, q_list->length) == 0) {
474 /* ok, it's a subclass somewhere on this branch */
477 offset = q_list->length;
479 q_list = q_list->down;
480 tail_lng = length - offset;
481 tail_qptr = (char *) qptr + offset;
484 if (length <= q_list->length) {
485 if (safe_strncmp (tail_qptr, (q_list->prefix) + offset, tail_lng)
487 /* same prefix: return the current class */
488 if (length == q_list->length)
491 /* found shorter common prefix */
493 /* add a node above q_list */
494 tmp = (struct q_class_t *) safe_calloc (1,
497 tmp->prefix = (char *) safe_calloc (1, length + 1);
498 strncpy (tmp->prefix, qptr, length);
499 tmp->length = length;
501 /* replace q_list by tmp */
503 tmp->next = q_list->next;
504 q_list->next->prev = tmp;
507 tmp->prev = q_list->prev;
508 q_list->prev->next = tmp;
511 /* make q_list a child of tmp */
513 tmp->up = q_list->up;
515 if (tmp->up->down == q_list)
518 /* q_list has no siblings */
522 index = q_list->index;
524 /* tmp should be the return class too */
527 /* next class to test */
531 /* found another branch for which tmp is a shorter prefix */
533 /* save the next sibling for later */
536 /* unlink q_list from the top level list */
538 q_list->next->prev = q_list->prev;
540 q_list->prev->next = q_list->next;
542 /* at this point, we have a tmp->down; link q_list to it */
551 index = q_list->index;
553 /* next class to test */
557 /* we found a shorter prefix, so we need a redraw */
562 q_list = q_list->next;
567 /* longer than the current prefix: try subclassing it */
569 && safe_strncmp (tail_qptr, (q_list->prefix) + offset,
570 q_list->length - offset) == 0) {
571 /* still a subclass: go down one level */
573 offset = q_list->length;
575 q_list = q_list->down;
576 tail_lng = length - offset;
577 tail_qptr = (char *) qptr + offset;
582 /* nope, try the next prefix */
583 q_list = q_list->next;
589 /* still not found so far: add it as a sibling to the current node */
592 (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
593 tmp->prefix = (char *) safe_calloc (1, length + 1);
594 strncpy (tmp->prefix, qptr, length);
595 tmp->length = length;
598 tmp->next = ptr->down;
599 ptr->down->prev = tmp;
604 new_class_color (tmp, q_level);
610 shift_class_colors (*QuoteList, tmp, index, q_level);
616 /* nope, try the next prefix */
617 q_list = q_list->next;
624 /* not found so far: add it as a top level class */
625 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
626 class->prefix = (char *) safe_calloc (1, length + 1);
627 strncpy (class->prefix, qptr, length);
628 class->length = length;
629 new_class_color (class, q_level);
632 class->next = *QuoteList;
633 (*QuoteList)->prev = class;
639 shift_class_colors (*QuoteList, tmp, index, q_level);
644 static int check_attachment_marker (char *);
647 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
648 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
651 COLOR_LINE *color_line;
652 regmatch_t pmatch[1], smatch[1];
653 int found, offset, null_rx, i;
655 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
657 lineInfo[n].type = MT_COLOR_NORMAL;
658 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
659 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
660 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
663 lineInfo[n].type = MT_COLOR_HDEFAULT;
664 color_line = ColorHdrList;
666 if (REGEXEC (&color_line->rx, buf) == 0) {
667 lineInfo[n].type = MT_COLOR_HEADER;
668 lineInfo[n].syntax[0].color = color_line->pair;
671 color_line = color_line->next;
675 else if (safe_strncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
676 lineInfo[n].type = MT_COLOR_NORMAL;
678 else if (safe_strncmp ("[-- ", buf, 4) == 0)
679 lineInfo[n].type = MT_COLOR_ATTACHMENT;
681 else if (check_attachment_marker ((char *) raw) == 0)
682 lineInfo[n].type = MT_COLOR_ATTACHMENT;
684 else if (safe_strcmp ("-- \n", buf) == 0
685 || safe_strcmp ("-- \r\n", buf) == 0) {
688 lineInfo[n].type = MT_COLOR_SIGNATURE;
689 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
690 (lineInfo[i].type == MT_COLOR_NORMAL ||
691 lineInfo[i].type == MT_COLOR_QUOTED ||
692 lineInfo[i].type == MT_COLOR_HEADER)) {
694 if (lineInfo[i].chunks) {
695 lineInfo[i].chunks = 0;
696 safe_realloc (&(lineInfo[n].syntax), sizeof (struct syntax_t));
698 lineInfo[i++].type = MT_COLOR_SIGNATURE;
701 else if (check_sig (buf, lineInfo, n - 1) == 0)
702 lineInfo[n].type = MT_COLOR_SIGNATURE;
703 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
704 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
705 if (smatch[0].rm_so > 0) {
708 /* hack to avoid making an extra copy of buf */
709 c = buf[smatch[0].rm_so];
710 buf[smatch[0].rm_so] = 0;
712 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
713 if (q_classify && lineInfo[n].quote == NULL)
714 lineInfo[n].quote = classify_quote (QuoteList,
715 buf + pmatch[0].rm_so,
717 pmatch[0].rm_so, force_redraw,
719 lineInfo[n].type = MT_COLOR_QUOTED;
722 lineInfo[n].type = MT_COLOR_NORMAL;
724 buf[smatch[0].rm_so] = c;
727 lineInfo[n].type = MT_COLOR_NORMAL;
730 if (q_classify && lineInfo[n].quote == NULL)
731 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
732 pmatch[0].rm_eo - pmatch[0].rm_so,
733 force_redraw, q_level);
734 lineInfo[n].type = MT_COLOR_QUOTED;
738 lineInfo[n].type = MT_COLOR_NORMAL;
741 if (lineInfo[n].type == MT_COLOR_NORMAL ||
742 lineInfo[n].type == MT_COLOR_QUOTED) {
746 lineInfo[n].chunks = 0;
753 color_line = ColorBodyList;
755 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
756 (offset ? REG_NOTBOL : 0)) == 0) {
757 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
759 if (++(lineInfo[n].chunks) > 1)
760 safe_realloc (&(lineInfo[n].syntax),
761 (lineInfo[n].chunks) *
762 sizeof (struct syntax_t));
764 i = lineInfo[n].chunks - 1;
765 pmatch[0].rm_so += offset;
766 pmatch[0].rm_eo += offset;
768 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
769 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
770 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
771 (lineInfo[n].syntax)[i].color = color_line->pair;
772 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
773 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
779 null_rx = 1; /* empty regexp; don't add it, but keep looking */
781 color_line = color_line->next;
785 offset++; /* avoid degenerate cases */
787 offset = (lineInfo[n].syntax)[i].last;
788 } while (found || null_rx);
792 static int is_ansi (unsigned char *buf)
794 while (*buf && (isdigit (*buf) || *buf == ';'))
796 return (*buf == 'm');
799 static int check_attachment_marker (char *p)
801 char *q = AttachmentMarker;
803 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
804 return (int) (*p - *q);
807 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
811 while (isdigit (buf[x]) || buf[x] == ';')
814 /* Character Attributes */
815 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
819 mutt_free_color (a->fg, a->bg);
825 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
826 a->attr |= ANSI_BOLD;
829 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
830 a->attr |= ANSI_UNDERLINE;
833 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
834 a->attr |= ANSI_BLINK;
837 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
838 a->attr |= ANSI_REVERSE;
841 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
844 mutt_free_color (a->fg, a->bg);
850 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
853 mutt_free_color (a->fg, a->bg);
856 a->attr |= ANSI_COLOR;
857 a->fg = buf[pos + 1] - '0';
860 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
863 mutt_free_color (a->fg, a->bg);
866 a->attr |= ANSI_COLOR;
867 a->bg = buf[pos + 1] - '0';
871 while (pos < x && buf[pos] != ';')
882 fill_buffer (FILE * f, long *last_pos, long offset, unsigned char *buf,
883 unsigned char *fmt, size_t blen, int *buf_ready)
886 static int b_read = 0;
888 if (*buf_ready == 0) {
890 if (offset != *last_pos)
891 fseek (f, offset, 0);
892 if (fgets ((char *) buf, blen - 1, f) == NULL) {
896 *last_pos = ftell (f);
897 b_read = (int) (*last_pos - offset);
900 /* copy "buf" to "fmt", but without bold and underline controls */
903 if (*p == '\010' && (p > buf)) {
904 if (*(p + 1) == '_') /* underline */
906 else if (*(p + 1)) { /* bold or overstrike */
907 *(fmt - 1) = *(p + 1);
913 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
914 while (*p++ != 'm') /* skip ANSI sequence */
917 else if (*p == '\033' && *(p + 1) == ']'
918 && check_attachment_marker ((char *) p) == 0) {
919 debug_print (2, ("seen attachment marker.\n"));
920 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
937 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
938 int flags, ansi_attr * pa, int cnt,
939 int *pspace, int *pvch, int *pcol, int *pspecial)
941 int space = -1; /* index of the last space or TAB */
942 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
943 int ch, vch, k, last_special = -1, special = 0, t;
947 int wrap_cols = COLS;
949 if (!(flags & (M_SHOWFLAT)))
950 wrap_cols -= WrapMargin;
951 wrap_cols -= SidebarWidth;
956 /* FIXME: this should come from lineInfo */
957 memset (&mbstate, 0, sizeof (mbstate));
959 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
960 /* Handle ANSI sequences */
961 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
962 is_ansi (buf + ch + 2))
963 ch = grok_ansi (buf, ch + 2, pa) + 1;
965 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
966 check_attachment_marker ((char *) buf + ch) == 0) {
967 while (buf[ch++] != '\a')
972 /* is anything left to do? */
976 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
977 if (k == -2 || k == -1) {
978 debug_print (1, ("mbrtowc returned %d; errno = %d.\n", k, errno));
979 if (col + 4 > wrap_cols)
983 printw ("\\%03o", buf[ch]);
990 /* Handle backspace */
997 while ((wc1 = 0, mbstate1 = mbstate,
999 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
1000 &mbstate1), k1 - k > 0 && wc1 == '\b')
1002 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1003 &mbstate1), k2 > 0 && IsWPrint (wc1))) {
1005 special |= (wc == '_' && special & A_UNDERLINE)
1006 ? A_UNDERLINE : A_BOLD;
1008 else if (wc == '_' || wc1 == '_') {
1009 special |= A_UNDERLINE;
1010 wc = (wc1 == '_') ? wc : wc1;
1013 /* special = 0; / * overstrike: nothing to do! */
1023 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1024 special || last_special || pa->attr)) {
1025 resolve_color (*lineInfo, n, vch, flags, special, pa);
1026 last_special = special;
1029 if (IsWPrint (wc)) {
1033 if (col + t > wrap_cols)
1039 else if (wc == '\n')
1041 else if (wc == '\t') {
1047 for (; col < t; col++)
1052 else if (wc < 0x20 || wc == 0x7f) {
1053 if (col + 2 > wrap_cols)
1057 printw ("^%c", ('@' + wc) & 0x7f);
1059 else if (wc < 0x100) {
1060 if (col + 4 > wrap_cols)
1064 printw ("\\%03o", wc);
1067 if (col + 1 > wrap_cols)
1071 addch (replacement_char ());
1077 *pspecial = special;
1083 * flags M_SHOWFLAT, show characters (used for displaying help)
1084 * M_SHOWCOLOR, show characters in color
1085 * otherwise don't show characters
1086 * M_HIDE, don't show quoted text
1087 * M_SEARCH, resolve search patterns
1088 * M_TYPES, compute line's type
1089 * M_PAGER_NSKIP, keeps leading whitespace
1090 * M_PAGER_MARKER, eventually show markers
1093 * -1 EOF was reached
1094 * 0 normal exit, line was not displayed
1095 * >0 normal exit, line was displayed
1099 display_line (FILE * f, long *last_pos, struct line_t **lineInfo, int n,
1100 int *last, int *max, int flags, struct q_class_t **QuoteList,
1101 int *q_level, int *force_redraw, regex_t * SearchRE)
1103 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1104 unsigned char *buf_ptr = buf;
1105 int ch, vch, col, cnt, b_read;
1106 int buf_ready = 0, change_last = 0;
1111 ansi_attr a = { 0, 0, 0, -1 };
1112 regmatch_t pmatch[1];
1119 if (*last == *max) {
1120 safe_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1121 for (ch = *last; ch < *max; ch++) {
1122 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1123 (*lineInfo)[ch].type = -1;
1124 (*lineInfo)[ch].search_cnt = -1;
1125 (*lineInfo)[ch].syntax = safe_malloc (sizeof (struct syntax_t));
1126 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1131 /* only do color hiliting if we are viewing a message */
1132 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1133 if ((*lineInfo)[n].type == -1) {
1134 /* determine the line class */
1136 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1143 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1144 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1146 /* avoid race condition for continuation lines when scrolling up */
1148 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1150 (*lineInfo)[m].type = (*lineInfo)[n].type;
1153 /* this also prevents searching through the hidden lines */
1154 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1155 flags = 0; /* M_NOSHOW */
1158 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1159 * don't want to compute it every time M_TYPES is set, since this
1160 * would slow down the "bottom" function unacceptably. A compromise
1161 * solution is hence to call regexec() again, just to find out the
1162 * length of the quote prefix.
1164 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1165 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1168 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1174 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1175 (*lineInfo)[n].quote = classify_quote (QuoteList,
1176 (char *) fmt + pmatch[0].rm_so,
1177 pmatch[0].rm_eo - pmatch[0].rm_so,
1178 force_redraw, q_level);
1181 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1182 && (*lineInfo)[n].search_cnt == -1) {
1184 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1192 (*lineInfo)[n].search_cnt = 0;
1194 (SearchRE, (char *) fmt + offset, 1, pmatch,
1195 (offset ? REG_NOTBOL : 0)) == 0) {
1196 if (++((*lineInfo)[n].search_cnt) > 1)
1197 safe_realloc (&((*lineInfo)[n].search),
1198 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1200 (*lineInfo)[n].search = safe_malloc (sizeof (struct syntax_t));
1201 pmatch[0].rm_so += offset;
1202 pmatch[0].rm_eo += offset;
1203 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1205 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1208 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1209 offset++; /* avoid degenerate cases */
1211 offset = pmatch[0].rm_eo;
1217 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1218 /* we've already scanned this line, so just exit */
1221 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1222 /* no need to try to display this line... */
1223 return (1); /* fake display */
1226 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1227 sizeof (buf), &buf_ready)) < 0) {
1233 /* now chose a good place to break the line */
1235 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1237 buf_ptr = buf + cnt;
1239 /* move the break point only if smart_wrap is set */
1240 if (option (OPTWRAP)) {
1242 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1243 && buf[cnt] != '\r') {
1245 /* skip trailing blanks */
1246 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1251 buf_ptr = buf + cnt; /* a very long word... */
1253 if (!(flags & M_PAGER_NSKIP))
1254 /* skip leading blanks on the next line too */
1255 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1259 if (*buf_ptr == '\r')
1261 if (*buf_ptr == '\n')
1264 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1265 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1266 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1268 /* if we don't need to display the line we are done */
1269 if (!(flags & M_SHOW))
1272 /* display the line */
1273 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1275 /* avoid a bug in ncurses... */
1276 #ifndef USE_SLANG_CURSES
1278 SETCOLOR (MT_COLOR_NORMAL);
1283 /* end the last color pattern (needed by S-Lang) */
1284 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1285 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1288 * Fill the blank space at the end of the line with the prevailing color.
1289 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1290 * to make sure to reset the color *after* that
1292 if (flags & M_SHOWCOLOR) {
1293 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1294 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1295 def_color = ((*lineInfo)[m].syntax)[0].color;
1297 def_color = ColorDefs[(*lineInfo)[m].type];
1299 attrset (def_color);
1301 bkgdset (def_color | ' ');
1305 /* ncurses always wraps lines when you get to the right side of the
1306 * screen, but S-Lang seems to only wrap if the next character is *not*
1309 #ifndef USE_SLANG_CURSES
1315 * reset the color back to normal. This *must* come after the
1316 * addch('\n'), otherwise the color for this line will not be
1317 * filled to the right margin.
1319 if (flags & M_SHOWCOLOR) {
1320 SETCOLOR (MT_COLOR_NORMAL);
1321 BKGDSET (MT_COLOR_NORMAL);
1324 /* build a return code */
1325 if (!(flags & M_SHOW))
1331 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1333 while (cur > 0 && nlines > 0) {
1335 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1342 static struct mapping_t PagerHelp[] = {
1343 {N_("Exit"), OP_EXIT},
1344 {N_("PrevPg"), OP_PREV_PAGE},
1345 {N_("NextPg"), OP_NEXT_PAGE},
1348 static struct mapping_t PagerHelpExtra[] = {
1349 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1350 {N_("Del"), OP_DELETE},
1351 {N_("Reply"), OP_REPLY},
1352 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1357 static struct mapping_t PagerNewsHelpExtra[] = {
1358 {N_("Post"), OP_POST},
1359 {N_("Followup"), OP_FOLLOWUP},
1360 {N_("Del"), OP_DELETE},
1361 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1368 /* This pager is actually not so simple as it once was. It now operates in
1369 two modes: one for viewing messages and the other for viewing help. These
1370 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1371 is there so that we can do operations on the current message without the
1372 need to pop back out to the main-menu. */
1374 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1376 static char searchbuf[STRING];
1377 char buffer[LONG_STRING];
1378 char helpstr[SHORT_STRING * 2];
1379 char tmphelp[SHORT_STRING * 2];
1380 int maxLine, lastLine = 0;
1381 struct line_t *lineInfo;
1382 struct q_class_t *QuoteList = NULL;
1383 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1384 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1386 int redraw = REDRAW_FULL;
1388 long last_pos = 0, last_offset = 0;
1389 int old_smart_wrap, old_markers;
1392 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1393 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1395 int bodyoffset = 1; /* offset of first line of real text */
1396 int statusoffset = 0; /* offset for the status bar */
1397 int helpoffset = LINES - 2; /* offset for the help bar. */
1398 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1400 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1401 int indexoffset = 0; /* offset for the PI */
1402 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1403 int indicator = indexlen / 3; /* the indicator line of the PI */
1404 int old_PagerIndexLines; /* some people want to resize it
1405 * while inside the pager... */
1411 if (!(flags & M_SHOWCOLOR))
1412 flags |= M_SHOWFLAT;
1414 if ((fp = fopen (fname, "r")) == NULL) {
1415 mutt_perror (fname);
1419 if (stat (fname, &sb) != 0) {
1420 mutt_perror (fname);
1426 /* Initialize variables */
1428 if (IsHeader (extra) && !extra->hdr->read) {
1429 Context->msgnotreadyet = extra->hdr->msgno;
1430 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1433 lineInfo = safe_malloc (sizeof (struct line_t) * (maxLine = LINES));
1434 for (i = 0; i < maxLine; i++) {
1435 memset (&lineInfo[i], 0, sizeof (struct line_t));
1436 lineInfo[i].type = -1;
1437 lineInfo[i].search_cnt = -1;
1438 lineInfo[i].syntax = safe_malloc (sizeof (struct syntax_t));
1439 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1442 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1443 if (IsHeader (extra)) {
1444 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1445 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1448 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1451 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1454 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1455 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1456 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1466 if (redraw & REDRAW_FULL) {
1467 SETCOLOR (MT_COLOR_NORMAL);
1468 /* clear() doesn't optimize screen redraws */
1472 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1473 indexlen = Context->vcount + 1;
1475 indexlen = PagerIndexLines;
1477 indicator = indexlen / 3;
1479 if (option (OPTSTATUSONTOP)) {
1481 statusoffset = IsHeader (extra) ? indexlen : 0;
1482 bodyoffset = statusoffset + 1;
1483 helpoffset = LINES - 2;
1484 bodylen = helpoffset - bodyoffset;
1485 if (!option (OPTHELP))
1491 statusoffset = LINES - 2;
1492 if (!option (OPTHELP))
1494 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1495 bodylen = statusoffset - bodyoffset;
1498 if (option (OPTHELP)) {
1499 SETCOLOR (MT_COLOR_STATUS);
1500 move (helpoffset, 0);
1501 mutt_paddstr (COLS, helpstr);
1502 SETCOLOR (MT_COLOR_NORMAL);
1505 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1506 if (Resize != NULL) {
1507 if ((SearchCompiled = Resize->SearchCompiled)) {
1509 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1510 SearchFlag = M_SEARCH;
1511 SearchBack = Resize->SearchBack;
1513 lines = Resize->line;
1514 redraw |= REDRAW_SIGWINCH;
1520 if (IsHeader (extra) && PagerIndexLines) {
1521 if (index == NULL) {
1522 /* only allocate the space if/when we need the index.
1523 Initialise the menu as per the main index */
1524 index = mutt_new_menu ();
1525 index->menu = MENU_MAIN;
1526 index->make_entry = index_make_entry;
1527 index->color = index_color;
1528 index->max = Context->vcount;
1529 index->current = extra->hdr->virtual;
1532 SETCOLOR (MT_COLOR_NORMAL);
1533 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1535 index->pagelen = indexlen - 1;
1537 /* some fudge to work out where abouts the indicator should go */
1538 if (index->current - indicator < 0)
1540 else if (index->max - index->current < index->pagelen - indicator)
1541 index->top = index->max - index->pagelen;
1543 index->top = index->current - indicator;
1545 menu_redraw_index (index);
1548 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1552 if (redraw & REDRAW_SIGWINCH) {
1555 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1556 has_types | SearchFlag, &QuoteList, &q_level,
1557 &force_redraw, &SearchRE) == 0) {
1558 if (!lineInfo[i].continuation && ++j == lines) {
1563 redraw |= REDRAW_SIDEBAR;
1567 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1569 move (bodyoffset, SidebarWidth);
1570 curline = oldtopline = topline;
1574 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1575 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1577 (flags & M_DISPLAYFLAGS) | hideQuoted |
1578 SearchFlag, &QuoteList, &q_level, &force_redraw,
1582 move (lines + bodyoffset, SidebarWidth);
1583 redraw |= REDRAW_SIDEBAR;
1585 last_offset = lineInfo[curline].offset;
1586 } while (force_redraw);
1588 SETCOLOR (MT_COLOR_TILDE);
1589 BKGDSET (MT_COLOR_TILDE);
1590 while (lines < bodylen) {
1592 if (option (OPTTILDE))
1596 move (lines + bodyoffset, SidebarWidth);
1598 /* We are going to update the pager status bar, so it isn't
1599 * necessary to reset to normal color now. */
1601 redraw |= REDRAW_STATUS; /* need to update the % seen */
1604 if (redraw & REDRAW_STATUS) {
1605 /* print out the pager status bar */
1606 SETCOLOR (MT_COLOR_STATUS);
1607 BKGDSET (MT_COLOR_STATUS);
1608 CLEARLINE_WIN (statusoffset);
1609 if (IsHeader (extra)) {
1610 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1611 size_t l2 = sizeof (buffer);
1613 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1614 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1616 else if (IsMsgAttach (extra)) {
1617 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1618 size_t l2 = sizeof (buffer);
1620 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1621 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1623 if (option(OPTSTATUSONTOP)) {
1626 /*move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (InHelp?(LINES-2):(indexlen - 1))),
1627 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);*/
1628 mutt_paddstr (COLS - 10 - (option(OPTSTATUSONTOP)?0:SidebarWidth), IsHeader (extra)
1629 || IsMsgAttach (extra) ? buffer : banner);
1632 if (last_pos < sb.st_size - 1)
1633 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1635 addstr (topline == 0 ? "all)" : "end)");
1636 BKGDSET (MT_COLOR_NORMAL);
1637 SETCOLOR (MT_COLOR_NORMAL);
1640 if (redraw & REDRAW_SIDEBAR)
1641 sidebar_draw (MENU_PAGER);
1643 if ((redraw & REDRAW_INDEX) && index) {
1644 /* redraw the pager_index indicator, because the
1645 * flags for this message might have changed. */
1646 menu_redraw_current (index);
1647 sidebar_draw (MENU_PAGER);
1648 /* print out the index status bar */
1649 menu_status_line (buffer, sizeof (buffer), index, NONULL (Status));
1650 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)),
1651 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);
1652 SETCOLOR (MT_COLOR_STATUS);
1653 mutt_paddstr (COLS - (option (OPTSTATUSONTOP) ? 0 : SidebarWidth),
1655 SETCOLOR (MT_COLOR_NORMAL);
1657 /* if we're not using the index, update every time */
1659 sidebar_draw (MENU_PAGER);
1663 move (statusoffset, COLS - 1);
1665 ch = km_dokey (MENU_PAGER);
1667 mutt_clear_error ();
1674 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1675 else if (SigWinch) {
1676 mutt_resize_screen ();
1678 /* Store current position. */
1680 for (i = 0; i <= topline; i++)
1681 if (!lineInfo[i].continuation)
1684 if (flags & M_PAGER_RETWINCH) {
1685 Resize = safe_malloc (sizeof (struct resize));
1687 Resize->line = lines;
1688 Resize->SearchCompiled = SearchCompiled;
1689 Resize->SearchBack = SearchBack;
1692 rc = OP_REFORMAT_WINCH;
1695 for (i = 0; i < maxLine; i++) {
1696 lineInfo[i].offset = 0;
1697 lineInfo[i].type = -1;
1698 lineInfo[i].continuation = 0;
1699 lineInfo[i].chunks = 0;
1700 lineInfo[i].search_cnt = -1;
1701 lineInfo[i].quote = NULL;
1703 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
1704 if (SearchCompiled && lineInfo[i].search)
1705 FREE (&(lineInfo[i].search));
1711 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1716 clearok (stdscr, TRUE); /*force complete redraw */
1720 else if (ch == -1) {
1734 if (lineInfo[curline].offset < sb.st_size - 1) {
1735 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1737 else if (option (OPTPAGERSTOP)) {
1738 /* emulate "less -q" and don't go on to the next message. */
1739 mutt_error _("Bottom of message is shown.");
1742 /* end of the current message, so display the next message. */
1743 rc = OP_MAIN_NEXT_UNDELETED;
1751 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1754 mutt_error _("Top of message is shown.");
1758 if (lineInfo[curline].offset < sb.st_size - 1) {
1761 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1767 mutt_error _("Bottom of message is shown.");
1772 topline = upNLines (1, lineInfo, topline, hideQuoted);
1774 mutt_error _("Top of message is shown.");
1781 mutt_error _("Top of message is shown.");
1786 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1788 mutt_error _("Top of message is shown.");
1792 if (lineInfo[curline].offset < sb.st_size - 1) {
1793 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1795 else if (option (OPTPAGERSTOP)) {
1796 /* emulate "less -q" and don't go on to the next message. */
1797 mutt_error _("Bottom of message is shown.");
1800 /* end of the current message, so display the next message. */
1801 rc = OP_MAIN_NEXT_UNDELETED;
1806 case OP_SEARCH_NEXT:
1807 case OP_SEARCH_OPPOSITE:
1808 if (SearchCompiled) {
1810 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1811 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1812 /* searching forward */
1813 for (i = topline + 1; i < lastLine; i++) {
1814 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1815 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1822 mutt_error _("Not found.");
1825 /* searching backward */
1826 for (i = topline - 1; i >= 0; i--) {
1827 if ((!hideQuoted || (has_types &&
1828 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1829 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1836 mutt_error _("Not found.");
1839 if (lineInfo[topline].search_cnt > 0)
1840 SearchFlag = M_SEARCH;
1844 /* no previous search pattern, so fall through to search */
1847 case OP_SEARCH_REVERSE:
1848 strfcpy (buffer, searchbuf, sizeof (buffer));
1849 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1850 _("Search: ")), buffer, sizeof (buffer),
1854 if (!strcmp (buffer, searchbuf)) {
1855 if (SearchCompiled) {
1856 /* do an implicit search-next */
1857 if (ch == OP_SEARCH)
1858 ch = OP_SEARCH_NEXT;
1860 ch = OP_SEARCH_OPPOSITE;
1869 strfcpy (searchbuf, buffer, sizeof (searchbuf));
1871 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1872 if (ch == OP_SEARCH)
1874 else if (ch == OP_SEARCH_REVERSE)
1877 if (SearchCompiled) {
1878 regfree (&SearchRE);
1879 for (i = 0; i < lastLine; i++) {
1880 if (lineInfo[i].search)
1881 FREE (&(lineInfo[i].search));
1882 lineInfo[i].search_cnt = -1;
1887 REGCOMP (&SearchRE, searchbuf,
1888 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1889 regerror (err, &SearchRE, buffer, sizeof (buffer));
1890 mutt_error ("%s", buffer);
1891 regfree (&SearchRE);
1892 for (i = 0; i < maxLine; i++) {
1894 if (lineInfo[i].search)
1895 FREE (&(lineInfo[i].search));
1896 lineInfo[i].search_cnt = -1;
1903 /* update the search pointers */
1905 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1906 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1907 &QuoteList, &q_level,
1908 &force_redraw, &SearchRE) == 0) {
1910 redraw |= REDRAW_SIDEBAR;
1914 /* searching forward */
1915 for (i = topline; i < lastLine; i++) {
1916 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1917 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1925 /* searching backward */
1926 for (i = topline; i >= 0; i--) {
1927 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1928 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1936 if (lineInfo[topline].search_cnt == 0) {
1938 mutt_error _("Not found.");
1941 SearchFlag = M_SEARCH;
1943 redraw = REDRAW_BODY;
1946 case OP_SEARCH_TOGGLE:
1947 if (SearchCompiled) {
1948 SearchFlag ^= M_SEARCH;
1949 redraw = REDRAW_BODY;
1954 /* don't let the user enter the help-menu from the help screen! */
1957 mutt_help (MENU_PAGER);
1958 redraw = REDRAW_FULL;
1962 mutt_error _("Help is currently being shown.");
1965 case OP_PAGER_HIDE_QUOTED:
1967 hideQuoted ^= M_HIDE;
1968 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1969 topline = upNLines (1, lineInfo, topline, hideQuoted);
1971 redraw = REDRAW_BODY;
1975 case OP_PAGER_SKIP_QUOTED:
1978 int new_topline = topline;
1980 while ((new_topline < lastLine ||
1981 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1982 new_topline, &lastLine,
1983 &maxLine, M_TYPES, &QuoteList,
1984 &q_level, &force_redraw,
1986 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
1987 redraw |= REDRAW_SIDEBAR;
1992 mutt_error _("No more quoted text.");
1997 while ((new_topline < lastLine ||
1998 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1999 new_topline, &lastLine,
2000 &maxLine, M_TYPES, &QuoteList,
2001 &q_level, &force_redraw,
2003 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2005 redraw |= REDRAW_SIDEBAR;
2009 mutt_error _("No more unquoted text after quoted text.");
2013 topline = new_topline;
2017 case OP_PAGER_BOTTOM: /* move to the end of the file */
2018 if (lineInfo[curline].offset < sb.st_size - 1) {
2020 /* make sure the types are defined to the end of file */
2021 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2022 &maxLine, has_types,
2023 &QuoteList, &q_level, &force_redraw,
2026 redraw |= REDRAW_SIDEBAR;
2028 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2031 mutt_error _("Bottom of message is shown.");
2035 clearok (stdscr, TRUE);
2036 redraw = REDRAW_FULL;
2040 km_error_key (MENU_PAGER);
2043 /* --------------------------------------------------------------------
2044 * The following are operations on the current message rather than
2045 * adjusting the view of the message.
2048 case OP_BOUNCE_MESSAGE:
2049 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2051 if (IsMsgAttach (extra))
2052 mutt_attach_bounce (extra->fp, extra->hdr,
2053 extra->idx, extra->idxlen, extra->bdy);
2055 ci_bounce_message (extra->hdr, &redraw);
2059 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2061 if (IsMsgAttach (extra))
2062 mutt_attach_resend (extra->fp, extra->hdr,
2063 extra->idx, extra->idxlen, extra->bdy);
2065 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2066 redraw = REDRAW_FULL;
2069 case OP_CHECK_TRADITIONAL:
2070 CHECK_MODE (IsHeader (extra));
2071 if (!(WithCrypto & APPLICATION_PGP))
2073 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2075 rc = OP_CHECK_TRADITIONAL;
2079 case OP_CREATE_ALIAS:
2080 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2081 if (IsMsgAttach (extra))
2082 mutt_create_alias (extra->bdy->hdr->env, NULL);
2084 mutt_create_alias (extra->hdr->env, NULL);
2085 MAYBE_REDRAW (redraw);
2088 case OP_PURGE_MESSAGE:
2090 CHECK_MODE (IsHeader (extra));
2093 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2095 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2096 mutt_set_flag (Context, extra->hdr, M_PURGED,
2097 ch != OP_PURGE_MESSAGE ? 0 : 1);
2098 if (option (OPTDELETEUNTAG))
2099 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2100 redraw = REDRAW_STATUS | REDRAW_INDEX;
2101 if (option (OPTRESOLVE)) {
2103 rc = OP_MAIN_NEXT_UNDELETED;
2107 case OP_DELETE_THREAD:
2108 case OP_DELETE_SUBTHREAD:
2109 CHECK_MODE (IsHeader (extra));
2112 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2114 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2115 ch == OP_DELETE_THREAD ? 0 : 1);
2118 if (option (OPTDELETEUNTAG))
2119 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2120 ch == OP_DELETE_THREAD ? 0 : 1);
2121 if (option (OPTRESOLVE)) {
2122 rc = OP_MAIN_NEXT_UNDELETED;
2126 if (!option (OPTRESOLVE) && PagerIndexLines)
2127 redraw = REDRAW_FULL;
2129 redraw = REDRAW_STATUS | REDRAW_INDEX;
2133 case OP_DISPLAY_ADDRESS:
2134 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2135 if (IsMsgAttach (extra))
2136 mutt_display_address (extra->bdy->hdr->env);
2138 mutt_display_address (extra->hdr->env);
2141 case OP_ENTER_COMMAND:
2142 old_smart_wrap = option (OPTWRAP);
2143 old_markers = option (OPTMARKERS);
2144 old_PagerIndexLines = PagerIndexLines;
2146 CurrentMenu = MENU_PAGER;
2147 mutt_enter_command ();
2149 if (option (OPTNEEDRESORT)) {
2150 unset_option (OPTNEEDRESORT);
2151 CHECK_MODE (IsHeader (extra));
2152 set_option (OPTNEEDRESORT);
2155 if (old_PagerIndexLines != PagerIndexLines) {
2157 mutt_menuDestroy (&index);
2161 if (option (OPTWRAP) != old_smart_wrap ||
2162 option (OPTMARKERS) != old_markers) {
2163 if (flags & M_PAGER_RETWINCH) {
2165 rc = OP_REFORMAT_WINCH;
2169 /* count the real lines above */
2171 for (i = 0; i <= topline; i++) {
2172 if (!lineInfo[i].continuation)
2176 /* we need to restart the whole thing */
2177 for (i = 0; i < maxLine; i++) {
2178 lineInfo[i].offset = 0;
2179 lineInfo[i].type = -1;
2180 lineInfo[i].continuation = 0;
2181 lineInfo[i].chunks = 0;
2182 lineInfo[i].search_cnt = -1;
2183 lineInfo[i].quote = NULL;
2185 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2186 if (SearchCompiled && lineInfo[i].search)
2187 FREE (&(lineInfo[i].search));
2190 if (SearchCompiled) {
2191 regfree (&SearchRE);
2196 /* try to keep the old position */
2199 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2200 &lastLine, &maxLine,
2201 (has_types ? M_TYPES : 0),
2202 &QuoteList, &q_level, &force_redraw,
2204 redraw |= REDRAW_SIDEBAR;
2205 if (!lineInfo[topline].continuation)
2214 if (option (OPTFORCEREDRAWPAGER))
2215 redraw = REDRAW_FULL;
2216 unset_option (OPTFORCEREDRAWINDEX);
2217 unset_option (OPTFORCEREDRAWPAGER);
2220 case OP_FLAG_MESSAGE:
2221 CHECK_MODE (IsHeader (extra));
2224 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2226 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2227 redraw = REDRAW_STATUS | REDRAW_INDEX;
2228 if (option (OPTRESOLVE)) {
2230 rc = OP_MAIN_NEXT_UNDELETED;
2235 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2236 if (IsAttach (extra))
2237 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2239 mutt_pipe_message (extra->hdr);
2240 MAYBE_REDRAW (redraw);
2244 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2245 if (IsAttach (extra))
2246 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2248 mutt_print_message (extra->hdr);
2252 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2254 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2255 redraw = REDRAW_FULL;
2260 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2262 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2263 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2264 query_quadoption (OPT_TOMODERATED,
2266 ("Posting to this group not allowed, may be moderated. Continue?"))
2269 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2270 redraw = REDRAW_FULL;
2273 case OP_FORWARD_TO_GROUP:
2274 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2276 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2277 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2278 query_quadoption (OPT_TOMODERATED,
2280 ("Posting to this group not allowed, may be moderated. Continue?"))
2283 if (IsMsgAttach (extra))
2284 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2285 extra->idxlen, extra->bdy, SENDNEWS);
2287 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2289 redraw = REDRAW_FULL;
2293 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2296 if (IsMsgAttach (extra))
2297 followup_to = extra->bdy->hdr->env->followup_to;
2299 followup_to = extra->hdr->env->followup_to;
2301 if (!followup_to || safe_strcasecmp (followup_to, "poster") ||
2302 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2303 _("Reply by mail as poster prefers?")) != M_YES) {
2304 if (extra->ctx && extra->ctx->magic == M_NNTP
2305 && !((NNTP_DATA *) extra->ctx->data)->allowed
2306 && query_quadoption (OPT_TOMODERATED,
2308 ("Posting to this group not allowed, may be moderated. Continue?"))
2311 if (IsMsgAttach (extra))
2312 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2313 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2315 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2316 extra->ctx, extra->hdr);
2317 redraw = REDRAW_FULL;
2323 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2325 if (IsMsgAttach (extra))
2326 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2327 extra->idxlen, extra->bdy, SENDREPLY);
2329 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2330 redraw = REDRAW_FULL;
2333 case OP_RECALL_MESSAGE:
2334 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2336 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2337 redraw = REDRAW_FULL;
2340 case OP_GROUP_REPLY:
2341 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2343 if (IsMsgAttach (extra))
2344 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2345 extra->idxlen, extra->bdy,
2346 SENDREPLY | SENDGROUPREPLY);
2348 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2350 redraw = REDRAW_FULL;
2354 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2356 if (IsMsgAttach (extra))
2357 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2358 extra->idxlen, extra->bdy,
2359 SENDREPLY | SENDLISTREPLY);
2361 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2363 redraw = REDRAW_FULL;
2366 case OP_FORWARD_MESSAGE:
2367 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2369 if (IsMsgAttach (extra))
2370 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2371 extra->idxlen, extra->bdy, 0);
2373 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2374 redraw = REDRAW_FULL;
2377 case OP_DECRYPT_SAVE:
2384 if (IsAttach (extra)) {
2385 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2390 case OP_COPY_MESSAGE:
2391 case OP_DECODE_SAVE:
2392 case OP_DECODE_COPY:
2393 case OP_DECRYPT_COPY:
2394 if (!WithCrypto && ch == OP_DECRYPT_COPY) {
2398 CHECK_MODE (IsHeader (extra));
2399 if (mutt_save_message (extra->hdr,
2400 (ch == OP_DECRYPT_SAVE) ||
2401 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2402 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2403 (ch == OP_DECRYPT_SAVE)
2404 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2405 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2406 || ch == OP_DECRYPT_SAVE)) {
2407 if (option (OPTRESOLVE)) {
2409 rc = OP_MAIN_NEXT_UNDELETED;
2412 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2414 MAYBE_REDRAW (redraw);
2417 case OP_SHELL_ESCAPE:
2418 mutt_shell_escape ();
2419 MAYBE_REDRAW (redraw);
2423 CHECK_MODE (IsHeader (extra));
2424 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2426 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2427 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2428 ? NULL : Context->last_tag);
2430 redraw = REDRAW_STATUS | REDRAW_INDEX;
2431 if (option (OPTRESOLVE)) {
2438 CHECK_MODE (IsHeader (extra));
2441 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2443 if (extra->hdr->read || extra->hdr->old)
2444 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2446 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2448 Context->msgnotreadyet = -1;
2449 redraw = REDRAW_STATUS | REDRAW_INDEX;
2450 if (option (OPTRESOLVE)) {
2452 rc = OP_MAIN_NEXT_UNDELETED;
2457 CHECK_MODE (IsHeader (extra));
2460 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2462 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2463 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2464 redraw = REDRAW_STATUS | REDRAW_INDEX;
2465 if (option (OPTRESOLVE)) {
2471 case OP_UNDELETE_THREAD:
2472 case OP_UNDELETE_SUBTHREAD:
2473 CHECK_MODE (IsHeader (extra));
2476 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2478 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2479 ch == OP_UNDELETE_THREAD ? 0 : 1)
2480 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2481 ch == OP_UNDELETE_THREAD ? 0 : 1);
2484 if (option (OPTRESOLVE)) {
2485 rc = (ch == OP_DELETE_THREAD) ?
2486 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2490 if (!option (OPTRESOLVE) && PagerIndexLines)
2491 redraw = REDRAW_FULL;
2493 redraw = REDRAW_STATUS | REDRAW_INDEX;
2503 redraw |= REDRAW_SIDEBAR;
2506 case OP_VIEW_ATTACHMENTS:
2507 if (flags & M_PAGER_ATTACHMENT) {
2509 rc = OP_ATTACH_COLLAPSE;
2512 CHECK_MODE (IsHeader (extra));
2513 mutt_view_attachments (extra->hdr);
2514 if (extra->hdr->attach_del)
2515 Context->changed = 1;
2516 redraw = REDRAW_FULL;
2521 if (!(WithCrypto & APPLICATION_PGP)) {
2525 CHECK_MODE (IsHeader (extra));
2527 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2528 redraw = REDRAW_FULL;
2532 case OP_FORGET_PASSPHRASE:
2533 crypt_forget_passphrase ();
2536 case OP_EXTRACT_KEYS:
2541 CHECK_MODE (IsHeader (extra));
2542 crypt_extract_keys_from_messages (extra->hdr);
2543 redraw = REDRAW_FULL;
2546 case OP_SIDEBAR_SCROLL_UP:
2547 case OP_SIDEBAR_SCROLL_DOWN:
2548 case OP_SIDEBAR_NEXT:
2549 case OP_SIDEBAR_NEXT_NEW:
2550 case OP_SIDEBAR_PREV:
2551 case OP_SIDEBAR_PREV_NEW:
2552 sidebar_scroll (ch, MENU_PAGER);
2561 if (IsHeader (extra))
2562 Context->msgnotreadyet = -1;
2564 cleanup_quote (&QuoteList);
2566 for (i = 0; i < maxLine; i++) {
2567 FREE (&(lineInfo[i].syntax));
2568 if (SearchCompiled && lineInfo[i].search)
2569 FREE (&(lineInfo[i].search));
2571 if (SearchCompiled) {
2572 regfree (&SearchRE);
2577 mutt_menuDestroy (&index);
2578 return (rc != -1 ? rc : 0);