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.
14 #include <lib-lib/lib-lib.h>
16 #include <lib-ui/curses.h>
17 #include <lib-ui/enter.h>
18 #include <lib-ui/menu.h>
19 #include <lib-ui/sidebar.h>
20 #include <lib-mx/mx.h>
28 #include "recvattach.h"
33 #include <imap/imap_private.h>
35 #include <lib-crypt/crypt.h>
37 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
39 #define IsAttach(x) (x && (x)->bdy)
40 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
41 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
42 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
43 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
44 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
46 static const char *Not_available_in_this_menu =
47 N_("Not available in this menu.");
48 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
49 static const char *Function_not_permitted_in_attach_message_mode =
50 N_("Function not permitted in attach-message mode.");
52 /* hack to return to position when returning from index to same message */
53 static int TopLine = 0;
54 static HEADER *OldHdr = NULL;
56 #define CHECK_MODE(x) if (!(x)) \
59 mutt_error _(Not_available_in_this_menu); \
63 #define CHECK_READONLY if (Context->readonly) \
66 mutt_error _(Mailbox_is_read_only); \
70 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
73 mutt_error _(Function_not_permitted_in_attach_message_mode); \
82 struct q_class_t *next, *prev;
83 struct q_class_t *down, *up;
98 struct syntax_t *syntax;
99 struct syntax_t *search;
100 struct q_class_t *quote;
103 #define ANSI_OFF (1<<0)
104 #define ANSI_BLINK (1<<1)
105 #define ANSI_BOLD (1<<2)
106 #define ANSI_UNDERLINE (1<<3)
107 #define ANSI_REVERSE (1<<4)
108 #define ANSI_COLOR (1<<5)
110 typedef struct _ansi_attr {
117 static short InHelp = 0;
119 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
120 static struct resize {
127 #define NumSigLines 4
129 static int check_sig (const char *s, struct line_t *info, int n)
133 while (n > 0 && count <= NumSigLines) {
134 if (info[n].type != MT_COLOR_SIGNATURE)
143 if (count > NumSigLines) {
144 /* check for a blank line */
158 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
159 int special, ansi_attr * a)
161 int def_color; /* color without syntax hilight */
162 int color; /* final color */
163 static int last_color; /* last color set */
164 int search = 0, i, m;
167 last_color = -1; /* force attrset() */
169 if (lineInfo[n].continuation) {
170 if (!cnt && option (OPTMARKERS)) {
171 SETCOLOR (MT_COLOR_MARKERS);
173 last_color = ColorDefs[MT_COLOR_MARKERS];
175 m = (lineInfo[n].syntax)[0].first;
176 cnt += (lineInfo[n].syntax)[0].last;
180 if (!(flags & M_SHOWCOLOR))
181 def_color = ColorDefs[MT_COLOR_NORMAL];
182 else if (lineInfo[m].type == MT_COLOR_HEADER)
183 def_color = (lineInfo[m].syntax)[0].color;
185 def_color = ColorDefs[lineInfo[m].type];
187 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
188 struct q_class_t *class = lineInfo[m].quote;
191 def_color = class->color;
193 while (class && class->length > cnt) {
194 def_color = class->color;
201 if (flags & M_SHOWCOLOR) {
202 for (i = 0; i < lineInfo[m].chunks; i++) {
203 /* we assume the chunks are sorted */
204 if (cnt > (lineInfo[m].syntax)[i].last)
206 if (cnt < (lineInfo[m].syntax)[i].first)
208 if (cnt != (lineInfo[m].syntax)[i].last) {
209 color = (lineInfo[m].syntax)[i].color;
212 /* don't break here, as cnt might be
213 * in the next chunk as well */
217 if (flags & M_SEARCH) {
218 for (i = 0; i < lineInfo[m].search_cnt; i++) {
219 if (cnt > (lineInfo[m].search)[i].last)
221 if (cnt < (lineInfo[m].search)[i].first)
223 if (cnt != (lineInfo[m].search)[i].last) {
224 color = ColorDefs[MT_COLOR_SEARCH];
231 /* handle "special" bold & underlined characters */
232 if (special || a->attr) {
234 if ((a->attr & ANSI_COLOR)) {
236 a->pair = mutt_alloc_color (a->fg, a->bg);
238 if (a->attr & ANSI_BOLD)
243 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
244 if (ColorDefs[MT_COLOR_BOLD] && !search)
245 color = ColorDefs[MT_COLOR_BOLD];
249 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
250 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
251 color = ColorDefs[MT_COLOR_UNDERLINE];
253 color ^= A_UNDERLINE;
255 else if (a->attr & ANSI_REVERSE) {
258 else if (a->attr & ANSI_BLINK) {
261 else if (a->attr & ANSI_OFF) {
266 if (color != last_color) {
272 static void append_line (struct line_t *lineInfo, int n, int cnt)
276 lineInfo[n + 1].type = lineInfo[n].type;
277 (lineInfo[n + 1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
278 lineInfo[n + 1].continuation = 1;
280 /* find the real start of the line */
281 for (m = n; m >= 0; m--)
282 if (lineInfo[m].continuation == 0)
285 (lineInfo[n + 1].syntax)[0].first = m;
286 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
287 cnt + (lineInfo[n].syntax)[0].last : cnt;
290 static void new_class_color (struct q_class_t *class, int *q_level)
292 class->index = (*q_level)++;
293 class->color = ColorQuote[class->index % ColorQuoteUsed];
297 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
298 int lindex, int *q_level)
300 struct q_class_t *q_list;
303 new_class->index = -1;
306 if (q_list->index >= lindex) {
308 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
311 q_list = q_list->down;
312 else if (q_list->next)
313 q_list = q_list->next;
315 while (!q_list->next) {
321 q_list = q_list->next;
325 new_class->index = lindex;
326 new_class->color = ColorQuote[lindex % ColorQuoteUsed];
330 static void cleanup_quote (struct q_class_t **QuoteList)
332 struct q_class_t *ptr;
335 if ((*QuoteList)->down)
336 cleanup_quote (&((*QuoteList)->down));
337 ptr = (*QuoteList)->next;
338 if ((*QuoteList)->prefix)
339 p_delete(&(*QuoteList)->prefix);
347 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
348 const char *qptr, int length,
349 int *force_redraw, int *q_level)
351 struct q_class_t *q_list = *QuoteList;
352 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
354 int offset, tail_lng;
357 if (ColorQuoteUsed <= 1) {
358 /* not much point in classifying quotes... */
360 if (*QuoteList == NULL) {
361 class = p_new(struct q_class_t, 1);
362 class->color = ColorQuote[0];
368 /* Did I mention how much I like emulating Lisp in C? */
370 /* classify quoting prefix */
372 if (length <= q_list->length) {
373 /* case 1: check the top level nodes */
375 if (m_strncmp(qptr, q_list->prefix, length) == 0) {
376 if (length == q_list->length)
377 return q_list; /* same prefix: return the current class */
379 /* found shorter prefix */
381 /* add a node above q_list */
382 tmp = p_new(struct q_class_t, 1);
383 tmp->prefix = p_dupstr(qptr, length);
384 tmp->length = length;
386 /* replace q_list by tmp in the top level list */
388 tmp->next = q_list->next;
389 q_list->next->prev = tmp;
392 tmp->prev = q_list->prev;
393 q_list->prev->next = tmp;
396 /* make q_list a child of tmp */
400 /* q_list has no siblings for now */
404 /* update the root if necessary */
405 if (q_list == *QuoteList)
408 lindex = q_list->index;
410 /* tmp should be the return class too */
413 /* next class to test; if tmp is a shorter prefix for another
414 * node, that node can only be in the top level list, so don't
415 * go down after this point
420 /* found another branch for which tmp is a shorter prefix */
422 /* save the next sibling for later */
425 /* unlink q_list from the top level list */
427 q_list->next->prev = q_list->prev;
429 q_list->prev->next = q_list->next;
431 /* at this point, we have a tmp->down; link q_list to it */
433 /* sibling order is important here, q_list should be linked last */
441 lindex = q_list->index;
443 /* next class to test; as above, we shouldn't go down */
447 /* we found a shorter prefix, so certain quotes have changed classes */
452 /* shorter, but not a substring of the current class: try next */
453 q_list = q_list->next;
458 /* case 2: try subclassing the current top level node */
460 /* tmp != NULL means we already found a shorter prefix at case 1 */
462 && m_strncmp(qptr, q_list->prefix, q_list->length) == 0) {
463 /* ok, it's a subclass somewhere on this branch */
466 offset = q_list->length;
468 q_list = q_list->down;
469 tail_lng = length - offset;
470 tail_qptr = (char *) qptr + offset;
473 if (length <= q_list->length) {
474 if (m_strncmp(tail_qptr, (q_list->prefix) + offset, tail_lng)
476 /* same prefix: return the current class */
477 if (length == q_list->length)
480 /* found shorter common prefix */
482 /* add a node above q_list */
483 tmp = p_new(struct q_class_t, 1);
484 tmp->prefix = p_dupstr(qptr, length);
485 tmp->length = length;
487 /* replace q_list by tmp */
489 tmp->next = q_list->next;
490 q_list->next->prev = tmp;
493 tmp->prev = q_list->prev;
494 q_list->prev->next = tmp;
497 /* make q_list a child of tmp */
499 tmp->up = q_list->up;
501 if (tmp->up->down == q_list)
504 /* q_list has no siblings */
508 lindex = q_list->index;
510 /* tmp should be the return class too */
513 /* next class to test */
517 /* found another branch for which tmp is a shorter prefix */
519 /* save the next sibling for later */
522 /* unlink q_list from the top level list */
524 q_list->next->prev = q_list->prev;
526 q_list->prev->next = q_list->next;
528 /* at this point, we have a tmp->down; link q_list to it */
537 lindex = q_list->index;
539 /* next class to test */
543 /* we found a shorter prefix, so we need a redraw */
548 q_list = q_list->next;
553 /* longer than the current prefix: try subclassing it */
555 && m_strncmp(tail_qptr, (q_list->prefix) + offset,
556 q_list->length - offset) == 0) {
557 /* still a subclass: go down one level */
559 offset = q_list->length;
561 q_list = q_list->down;
562 tail_lng = length - offset;
563 tail_qptr = (char *) qptr + offset;
568 /* nope, try the next prefix */
569 q_list = q_list->next;
575 /* still not found so far: add it as a sibling to the current node */
577 tmp = p_new(struct q_class_t, 1);
578 tmp->prefix = p_dupstr(qptr, length);
579 tmp->length = length;
582 tmp->next = ptr->down;
583 ptr->down->prev = tmp;
588 new_class_color (tmp, q_level);
594 shift_class_colors (*QuoteList, tmp, lindex, q_level);
600 /* nope, try the next prefix */
601 q_list = q_list->next;
608 /* not found so far: add it as a top level class */
609 class = p_new(struct q_class_t, 1);
610 class->prefix = p_dupstr(qptr, length);
611 class->length = length;
612 new_class_color (class, q_level);
615 class->next = *QuoteList;
616 (*QuoteList)->prev = class;
622 shift_class_colors (*QuoteList, tmp, lindex, q_level);
627 static int brailleLine = -1;
628 static int brailleCol = -1;
630 static int check_attachment_marker (char *);
633 resolve_types (char *buf, char *rawbuf, struct line_t *lineInfo, int n, int last,
634 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
637 COLOR_LINE *color_line;
638 regmatch_t pmatch[1], smatch[1];
639 int found, offset, null_rx, i;
641 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
642 if (buf[0] == '\n') {
643 lineInfo[n].type = MT_COLOR_NORMAL;
644 getyx(stdscr, brailleLine, brailleCol);
646 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
647 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
648 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
651 lineInfo[n].type = MT_COLOR_HDEFAULT;
652 color_line = ColorHdrList;
654 if (REGEXEC (&color_line->rx, buf) == 0) {
655 lineInfo[n].type = MT_COLOR_HEADER;
656 lineInfo[n].syntax[0].color = color_line->pair;
659 color_line = color_line->next;
663 else if (m_strncmp("\033[0m", rawbuf, 4) == 0) /* a little hack... */
664 lineInfo[n].type = MT_COLOR_NORMAL;
665 else if (check_attachment_marker ((char *) rawbuf) == 0)
666 lineInfo[n].type = MT_COLOR_ATTACHMENT;
667 else if (m_strcmp("-- \n", buf) == 0
668 || m_strcmp("-- \r\n", buf) == 0) {
671 lineInfo[n].type = MT_COLOR_SIGNATURE;
672 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
673 (lineInfo[i].type == MT_COLOR_NORMAL ||
674 lineInfo[i].type == MT_COLOR_QUOTED ||
675 lineInfo[i].type == MT_COLOR_HEADER)) {
677 if (lineInfo[i].chunks) {
678 lineInfo[i].chunks = 0;
679 p_realloc(&(lineInfo[n].syntax), 1);
681 lineInfo[i++].type = MT_COLOR_SIGNATURE;
684 else if (check_sig (buf, lineInfo, n - 1) == 0)
685 lineInfo[n].type = MT_COLOR_SIGNATURE;
686 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
687 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
688 if (smatch[0].rm_so > 0) {
691 /* hack to avoid making an extra copy of buf */
692 c = buf[smatch[0].rm_so];
693 buf[smatch[0].rm_so] = 0;
695 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
696 if (q_classify && lineInfo[n].quote == NULL)
697 lineInfo[n].quote = classify_quote (QuoteList,
698 buf + pmatch[0].rm_so,
700 pmatch[0].rm_so, force_redraw,
702 lineInfo[n].type = MT_COLOR_QUOTED;
705 lineInfo[n].type = MT_COLOR_NORMAL;
707 buf[smatch[0].rm_so] = c;
710 lineInfo[n].type = MT_COLOR_NORMAL;
713 if (q_classify && lineInfo[n].quote == NULL)
714 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
715 pmatch[0].rm_eo - pmatch[0].rm_so,
716 force_redraw, q_level);
717 lineInfo[n].type = MT_COLOR_QUOTED;
721 lineInfo[n].type = MT_COLOR_NORMAL;
724 if (lineInfo[n].type == MT_COLOR_NORMAL ||
725 lineInfo[n].type == MT_COLOR_QUOTED) {
729 lineInfo[n].chunks = 0;
736 color_line = ColorBodyList;
738 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
739 (offset ? REG_NOTBOL : 0)) == 0) {
740 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
742 if (++(lineInfo[n].chunks) > 1)
743 p_realloc(&(lineInfo[n].syntax), lineInfo[n].chunks);
745 i = lineInfo[n].chunks - 1;
746 pmatch[0].rm_so += offset;
747 pmatch[0].rm_eo += offset;
749 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
750 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
751 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
752 (lineInfo[n].syntax)[i].color = color_line->pair;
753 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
754 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
760 null_rx = 1; /* empty regexp; don't add it, but keep looking */
762 color_line = color_line->next;
766 offset++; /* avoid degenerate cases */
768 offset = (lineInfo[n].syntax)[i].last;
769 } while (found || null_rx);
773 static int is_ansi (unsigned char *buf)
775 while (*buf && (isdigit (*buf) || *buf == ';'))
777 return (*buf == 'm');
780 static int check_attachment_marker (char *p)
782 char *q = AttachmentMarker;
784 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
785 return (int) (*p - *q);
788 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
792 while (isdigit (buf[x]) || buf[x] == ';')
795 /* Character Attributes */
796 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
800 mutt_free_color (a->fg, a->bg);
808 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
809 a->attr |= ANSI_BOLD;
812 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
813 a->attr |= ANSI_UNDERLINE;
816 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
817 a->attr |= ANSI_BLINK;
820 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
821 a->attr |= ANSI_REVERSE;
824 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
829 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
832 mutt_free_color (a->fg, a->bg);
835 a->attr |= ANSI_COLOR;
836 if (buf[pos + 1] != '9')
837 a->fg = buf[pos + 1] - '0';
840 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
843 mutt_free_color (a->fg, a->bg);
846 a->attr |= ANSI_COLOR;
847 if (buf[pos + 1] != '9')
848 a->bg = buf[pos + 1] - '0';
852 while (pos < x && buf[pos] != ';')
862 /* trim tail of buf so that it contains complete multibyte characters */
863 static int trim_incomplete_mbyte(unsigned char *buf, size_t len) {
867 p_clear(&mbstate, 1);
868 for (; len > 0; buf += k, len -= k) {
869 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
872 else if (k == -1 || k == 0)
881 fill_buffer (FILE * f, off_t *last_pos, off_t offset, unsigned char *buf,
882 unsigned char *fmt, ssize_t blen, int *buf_ready)
885 static int b_read = 0;
887 if (*buf_ready == 0) {
889 if (offset != *last_pos)
890 fseeko (f, offset, 0);
891 if (fgets ((char *) buf, blen - 1, f) == NULL) {
895 *last_pos = ftello (f);
896 b_read = (int) (*last_pos - offset);
899 /* incomplete mbyte characters trigger a segfault in regex processing for
900 * certain versions of glibc. Trim them if necessary. */
901 if (b_read == blen - 2)
902 b_read -= trim_incomplete_mbyte(buf, b_read);
904 /* copy "buf" to "fmt", but without bold and underline controls */
907 if (*p == '\010' && (p > buf)) {
908 if (*(p + 1) == '_') /* underline */
910 else if (*(p + 1)) { /* bold or overstrike */
911 *(fmt - 1) = *(p + 1);
917 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
918 while (*p++ != 'm') /* skip ANSI sequence */
921 else if (*p == '\033' && *(p + 1) == ']'
922 && check_attachment_marker ((char *) p) == 0) {
923 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
935 #include <nntp/nntp.h>
938 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
939 int flags, ansi_attr * pa, int cnt,
940 int *pspace, int *pvch, int *pcol, int *pspecial)
942 int space = -1; /* index of the last space or TAB */
943 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
944 int ch, vch, k, last_special = -1, special = 0, t;
948 int wrap_cols = COLS;
950 if (!(flags & (M_SHOWFLAT)))
951 wrap_cols -= WrapMargin;
957 /* FIXME: this should come from lineInfo */
958 p_clear(&mbstate, 1);
960 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
961 /* Handle ANSI sequences */
962 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
963 is_ansi (buf + ch + 2))
964 ch = grok_ansi (buf, ch + 2, pa) + 1;
966 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
967 check_attachment_marker ((char *) buf + ch) == 0) {
968 while (buf[ch++] != '\a')
973 /* is anything left to do? */
977 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
978 if (k == -2 || k == -1) {
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;
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 (CharsetReplacement);
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, off_t *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 p_realloc(lineInfo, *max += LINES);
1121 for (ch = *last; ch < *max; ch++) {
1122 p_clear(&(*lineInfo)[ch], 1);
1123 (*lineInfo)[ch].type = -1;
1124 (*lineInfo)[ch].search_cnt = -1;
1125 (*lineInfo)[ch].syntax = p_new(struct syntax_t, 1);
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 p_realloc(&(*lineInfo)[n].search, (*lineInfo)[n].search_cnt);
1199 (*lineInfo)[n].search = p_new(struct syntax_t, 1);
1200 pmatch[0].rm_so += offset;
1201 pmatch[0].rm_eo += offset;
1202 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1204 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1207 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1208 offset++; /* avoid degenerate cases */
1210 offset = pmatch[0].rm_eo;
1216 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1217 /* we've already scanned this line, so just exit */
1220 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1221 /* no need to try to display this line... */
1222 return (1); /* fake display */
1225 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1226 sizeof (buf), &buf_ready)) < 0) {
1232 /* now chose a good place to break the line */
1234 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1236 buf_ptr = buf + cnt;
1238 /* move the break point only if smart_wrap is set */
1239 if (option (OPTWRAP)) {
1241 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1242 && buf[cnt] != '\r') {
1244 /* skip trailing blanks */
1245 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1247 /* a very long word with leading spaces causes infinite wrapping */
1248 if ((!ch) && (flags & M_PAGER_NSKIP))
1249 buf_ptr = buf + cnt;
1254 buf_ptr = buf + cnt; /* a very long word... */
1256 if (!(flags & M_PAGER_NSKIP))
1257 /* skip leading blanks on the next line too */
1258 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1262 if (*buf_ptr == '\r')
1264 if (*buf_ptr == '\n')
1267 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1268 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1269 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1271 /* if we don't need to display the line we are done */
1272 if (!(flags & M_SHOW))
1275 /* display the line */
1276 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1278 /* avoid a bug in ncurses... */
1279 #ifndef USE_SLANG_CURSES
1281 SETCOLOR (MT_COLOR_NORMAL);
1286 /* end the last color pattern (needed by S-Lang) */
1287 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1288 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1291 * Fill the blank space at the end of the line with the prevailing color.
1292 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1293 * to make sure to reset the color *after* that
1295 if (flags & M_SHOWCOLOR) {
1296 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1297 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1298 def_color = ((*lineInfo)[m].syntax)[0].color;
1300 def_color = ColorDefs[(*lineInfo)[m].type];
1302 attrset (def_color);
1304 bkgdset (def_color | ' ');
1308 /* ncurses always wraps lines when you get to the right side of the
1309 * screen, but S-Lang seems to only wrap if the next character is *not*
1312 #ifndef USE_SLANG_CURSES
1318 * reset the color back to normal. This *must* come after the
1319 * addch('\n'), otherwise the color for this line will not be
1320 * filled to the right margin.
1322 if (flags & M_SHOWCOLOR) {
1323 SETCOLOR (MT_COLOR_NORMAL);
1324 BKGDSET (MT_COLOR_NORMAL);
1327 /* build a return code */
1328 if (!(flags & M_SHOW))
1334 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1336 while (cur > 0 && nlines > 0) {
1338 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1345 static struct mapping_t PagerHelp[] = {
1346 {N_("Exit"), OP_EXIT},
1347 {N_("PrevPg"), OP_PREV_PAGE},
1348 {N_("NextPg"), OP_NEXT_PAGE},
1351 static struct mapping_t PagerHelpExtra[] = {
1352 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1353 {N_("Del"), OP_DELETE},
1354 {N_("Reply"), OP_REPLY},
1355 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1360 static struct mapping_t PagerNewsHelpExtra[] = {
1361 {N_("Post"), OP_POST},
1362 {N_("Followup"), OP_FOLLOWUP},
1363 {N_("Del"), OP_DELETE},
1364 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1371 /* This pager is actually not so simple as it once was. It now operates in
1372 two modes: one for viewing messages and the other for viewing help. These
1373 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1374 is there so that we can do operations on the current message without the
1375 need to pop back out to the main-menu. */
1377 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1379 static char searchbuf[STRING];
1380 char buffer[LONG_STRING];
1381 char helpstr[SHORT_STRING * 2];
1382 char tmphelp[SHORT_STRING * 2];
1383 int maxLine, lastLine = 0;
1384 struct line_t *lineInfo;
1385 struct q_class_t *QuoteList = NULL;
1386 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1387 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1389 int redraw = REDRAW_FULL;
1391 off_t last_pos = 0, last_offset = 0;
1392 int old_smart_wrap, old_markers;
1395 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1396 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1398 int bodyoffset = 1; /* offset of first line of real text */
1399 int statusoffset = 0; /* offset for the status bar */
1400 int helpoffset = LINES - 2; /* offset for the help bar. */
1401 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1403 MUTTMENU *pager_index = NULL; /* the Pager Index (PI) */
1404 int indexoffset = 0; /* offset for the PI */
1405 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1406 int indicator = indexlen / 3; /* the indicator line of the PI */
1407 int old_PagerIndexLines; /* some people want to resize it
1408 * while inside the pager... */
1414 if (!(flags & M_SHOWCOLOR))
1415 flags |= M_SHOWFLAT;
1417 if ((fp = fopen (fname, "r")) == NULL) {
1418 mutt_perror (fname);
1422 if (stat (fname, &sb) != 0) {
1423 mutt_perror (fname);
1429 /* Initialize variables */
1431 if (IsHeader (extra) && !extra->hdr->read) {
1432 Context->msgnotreadyet = extra->hdr->msgno;
1433 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1436 lineInfo = p_new(struct line_t, maxLine = LINES);
1437 for (i = 0; i < maxLine; i++) {
1438 p_clear(&lineInfo[i], 1);
1439 lineInfo[i].type = -1;
1440 lineInfo[i].search_cnt = -1;
1441 lineInfo[i].syntax = p_new(struct syntax_t, 1);
1442 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1445 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1446 if (IsHeader (extra)) {
1447 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1448 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1451 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1454 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1457 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1458 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1459 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, SW);
1501 mutt_paddstr (COLS-SW, 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 (pager_index == NULL) {
1522 /* only allocate the space if/when we need the index.
1523 Initialise the menu as per the main index */
1524 pager_index = mutt_new_menu ();
1525 pager_index->menu = MENU_MAIN;
1526 pager_index->make_entry = index_make_entry;
1527 pager_index->color = index_color;
1528 pager_index->max = Context->vcount;
1529 pager_index->current = extra->hdr->virtual;
1532 SETCOLOR (MT_COLOR_NORMAL);
1533 pager_index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1535 pager_index->pagelen = indexlen - 1;
1537 /* some fudge to work out where abouts the indicator should go */
1538 if (pager_index->current - indicator < 0)
1539 pager_index->top = 0;
1540 else if (pager_index->max - pager_index->current < pager_index->pagelen - indicator)
1541 pager_index->top = pager_index->max - pager_index->pagelen;
1543 pager_index->top = pager_index->current - indicator;
1545 menu_redraw_index (pager_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, SW);
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, SW);
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, SW);
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 move(statusoffset,SW);
1624 mutt_paddstr (COLS - 10 - SW, IsHeader (extra) ||
1625 IsMsgAttach (extra) ? buffer : banner);
1628 if (last_pos < sb.st_size - 1)
1629 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1631 addstr (topline == 0 ? "all)" : "end)");
1632 BKGDSET (MT_COLOR_NORMAL);
1633 SETCOLOR (MT_COLOR_NORMAL);
1636 if (redraw & REDRAW_SIDEBAR)
1639 if ((redraw & REDRAW_INDEX) && pager_index) {
1640 /* redraw the pager_index indicator, because the
1641 * flags for this message might have changed. */
1642 menu_redraw_current (pager_index);
1644 /* print out the pager_index status bar */
1645 menu_status_line (buffer, sizeof (buffer), pager_index, NONULL (Status));
1646 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), SW);
1647 SETCOLOR (MT_COLOR_STATUS);
1648 BKGDSET (MT_COLOR_STATUS);
1649 mutt_paddstr (COLS-SW, buffer);
1650 SETCOLOR (MT_COLOR_NORMAL);
1651 BKGDSET (MT_COLOR_NORMAL);
1653 /* if we're not using the index, update every time */
1659 if (option(OPTBRAILLEFRIENDLY)) {
1660 if (brailleLine!=-1) {
1661 move(brailleLine+1, 0);
1665 move (statusoffset, COLS-1);
1668 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1669 && lineInfo[curline].offset < sb.st_size-1) {
1670 if (TopLine - topline > lines)
1679 ch = km_dokey (MENU_PAGER);
1681 mutt_clear_error ();
1688 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1689 else if (SigWinch) {
1690 mutt_resize_screen ();
1692 /* Store current position. */
1694 for (i = 0; i <= topline; i++)
1695 if (!lineInfo[i].continuation)
1698 if (flags & M_PAGER_RETWINCH) {
1699 Resize = p_new(struct resize, 1);
1701 Resize->line = lines;
1702 Resize->SearchCompiled = SearchCompiled;
1703 Resize->SearchBack = SearchBack;
1706 rc = OP_REFORMAT_WINCH;
1709 for (i = 0; i < maxLine; i++) {
1710 lineInfo[i].offset = 0;
1711 lineInfo[i].type = -1;
1712 lineInfo[i].continuation = 0;
1713 lineInfo[i].chunks = 0;
1714 lineInfo[i].search_cnt = -1;
1715 lineInfo[i].quote = NULL;
1717 p_realloc(&lineInfo[i].syntax, 1);
1718 if (SearchCompiled && lineInfo[i].search)
1719 p_delete(&(lineInfo[i].search));
1725 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1730 clearok (stdscr, TRUE); /*force complete redraw */
1734 else if (ch == -1) {
1748 if (lineInfo[curline].offset < sb.st_size - 1) {
1749 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1751 else if (option (OPTPAGERSTOP)) {
1752 /* emulate "less -q" and don't go on to the next message. */
1753 mutt_error _("Bottom of message is shown.");
1756 /* end of the current message, so display the next message. */
1757 rc = OP_MAIN_NEXT_UNDELETED;
1765 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1768 mutt_error _("Top of message is shown.");
1772 if (lineInfo[curline].offset < sb.st_size - 1) {
1775 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1781 mutt_error _("Bottom of message is shown.");
1786 topline = upNLines (1, lineInfo, topline, hideQuoted);
1788 mutt_error _("Top of message is shown.");
1795 mutt_error _("Top of message is shown.");
1800 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1802 mutt_error _("Top of message is shown.");
1806 if (lineInfo[curline].offset < sb.st_size - 1) {
1807 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1809 else if (option (OPTPAGERSTOP)) {
1810 /* emulate "less -q" and don't go on to the next message. */
1811 mutt_error _("Bottom of message is shown.");
1814 /* end of the current message, so display the next message. */
1815 rc = OP_MAIN_NEXT_UNDELETED;
1820 case OP_SEARCH_NEXT:
1821 case OP_SEARCH_OPPOSITE:
1822 if (SearchCompiled) {
1824 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1825 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1826 /* searching forward */
1827 for (i = topline + 1; i < lastLine; i++) {
1828 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1829 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1836 mutt_error _("Not found.");
1839 /* searching backward */
1840 for (i = topline - 1; i >= 0; i--) {
1841 if ((!hideQuoted || (has_types &&
1842 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1843 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1850 mutt_error _("Not found.");
1853 if (lineInfo[topline].search_cnt > 0)
1854 SearchFlag = M_SEARCH;
1858 /* no previous search pattern, so fall through to search */
1861 case OP_SEARCH_REVERSE:
1862 m_strcpy(buffer, sizeof(buffer), searchbuf);
1863 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1864 _("Search: ")), buffer, sizeof (buffer),
1868 if (!strcmp (buffer, searchbuf)) {
1869 if (SearchCompiled) {
1870 /* do an implicit search-next */
1871 if (ch == OP_SEARCH)
1872 ch = OP_SEARCH_NEXT;
1874 ch = OP_SEARCH_OPPOSITE;
1883 m_strcpy(searchbuf, sizeof(searchbuf), buffer);
1885 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1886 if (ch == OP_SEARCH)
1888 else if (ch == OP_SEARCH_REVERSE)
1891 if (SearchCompiled) {
1892 regfree (&SearchRE);
1893 for (i = 0; i < lastLine; i++) {
1894 if (lineInfo[i].search)
1895 p_delete(&(lineInfo[i].search));
1896 lineInfo[i].search_cnt = -1;
1901 REGCOMP (&SearchRE, searchbuf,
1902 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1903 regerror (err, &SearchRE, buffer, sizeof (buffer));
1904 mutt_error ("%s", buffer);
1905 regfree (&SearchRE);
1906 for (i = 0; i < maxLine; i++) {
1908 if (lineInfo[i].search)
1909 p_delete(&(lineInfo[i].search));
1910 lineInfo[i].search_cnt = -1;
1917 /* update the search pointers */
1919 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1920 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1921 &QuoteList, &q_level,
1922 &force_redraw, &SearchRE) == 0) {
1924 redraw |= REDRAW_SIDEBAR;
1928 /* searching forward */
1929 for (i = topline; i < lastLine; i++) {
1930 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1931 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1939 /* searching backward */
1940 for (i = topline; i >= 0; i--) {
1941 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1942 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1950 if (lineInfo[topline].search_cnt == 0) {
1952 mutt_error _("Not found.");
1955 SearchFlag = M_SEARCH;
1957 redraw = REDRAW_BODY;
1960 case OP_SEARCH_TOGGLE:
1961 if (SearchCompiled) {
1962 SearchFlag ^= M_SEARCH;
1963 redraw = REDRAW_BODY;
1968 /* don't let the user enter the help-menu from the help screen! */
1971 mutt_help (MENU_PAGER);
1972 redraw = REDRAW_FULL;
1976 mutt_error _("Help is currently being shown.");
1979 case OP_PAGER_HIDE_QUOTED:
1981 hideQuoted ^= M_HIDE;
1982 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1983 topline = upNLines (1, lineInfo, topline, hideQuoted);
1985 redraw = REDRAW_BODY;
1989 case OP_PAGER_SKIP_QUOTED:
1992 int new_topline = topline;
1994 while ((new_topline < lastLine ||
1995 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1996 new_topline, &lastLine,
1997 &maxLine, M_TYPES, &QuoteList,
1998 &q_level, &force_redraw,
2000 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
2001 redraw |= REDRAW_SIDEBAR;
2006 mutt_error _("No more quoted text.");
2011 while ((new_topline < lastLine ||
2012 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2013 new_topline, &lastLine,
2014 &maxLine, M_TYPES, &QuoteList,
2015 &q_level, &force_redraw,
2017 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2019 redraw |= REDRAW_SIDEBAR;
2023 mutt_error _("No more unquoted text after quoted text.");
2027 topline = new_topline;
2031 case OP_PAGER_BOTTOM: /* move to the end of the file */
2032 if (lineInfo[curline].offset < sb.st_size - 1) {
2034 /* make sure the types are defined to the end of file */
2035 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2036 &maxLine, has_types,
2037 &QuoteList, &q_level, &force_redraw,
2040 redraw |= REDRAW_SIDEBAR;
2042 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2045 mutt_error _("Bottom of message is shown.");
2049 clearok (stdscr, TRUE);
2050 redraw = REDRAW_FULL;
2054 km_error_key (MENU_PAGER);
2057 /* --------------------------------------------------------------------
2058 * The following are operations on the current message rather than
2059 * adjusting the view of the message.
2062 case OP_BOUNCE_MESSAGE:
2063 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2065 if (IsMsgAttach (extra))
2066 mutt_attach_bounce (extra->fp, extra->hdr,
2067 extra->idx, extra->idxlen, extra->bdy);
2069 ci_bounce_message (extra->hdr, &redraw);
2073 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2075 if (IsMsgAttach (extra))
2076 mutt_attach_resend (extra->fp, extra->hdr,
2077 extra->idx, extra->idxlen, extra->bdy);
2079 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2080 redraw = REDRAW_FULL;
2083 case OP_CHECK_TRADITIONAL:
2084 CHECK_MODE (IsHeader (extra));
2085 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2087 rc = OP_CHECK_TRADITIONAL;
2091 case OP_CREATE_ALIAS:
2092 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2093 if (IsMsgAttach (extra))
2094 mutt_create_alias (extra->bdy->hdr->env, NULL);
2096 mutt_create_alias (extra->hdr->env, NULL);
2097 MAYBE_REDRAW (redraw);
2100 case OP_PURGE_MESSAGE:
2102 CHECK_MODE (IsHeader (extra));
2105 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2107 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2108 mutt_set_flag (Context, extra->hdr, M_PURGED,
2109 ch != OP_PURGE_MESSAGE ? 0 : 1);
2110 if (option (OPTDELETEUNTAG))
2111 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2112 redraw = REDRAW_STATUS | REDRAW_INDEX;
2113 if (option (OPTRESOLVE)) {
2115 rc = OP_MAIN_NEXT_UNDELETED;
2119 case OP_DELETE_THREAD:
2120 case OP_DELETE_SUBTHREAD:
2121 CHECK_MODE (IsHeader (extra));
2124 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2126 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2127 ch == OP_DELETE_THREAD ? 0 : 1);
2130 if (option (OPTDELETEUNTAG))
2131 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2132 ch == OP_DELETE_THREAD ? 0 : 1);
2133 if (option (OPTRESOLVE)) {
2134 rc = OP_MAIN_NEXT_UNDELETED;
2138 if (!option (OPTRESOLVE) && PagerIndexLines)
2139 redraw = REDRAW_FULL;
2141 redraw = REDRAW_STATUS | REDRAW_INDEX;
2145 case OP_DISPLAY_ADDRESS:
2146 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2147 if (IsMsgAttach (extra))
2148 mutt_display_address (extra->bdy->hdr->env);
2150 mutt_display_address (extra->hdr->env);
2153 case OP_ENTER_COMMAND:
2154 old_smart_wrap = option (OPTWRAP);
2155 old_markers = option (OPTMARKERS);
2156 old_PagerIndexLines = PagerIndexLines;
2158 CurrentMenu = MENU_PAGER;
2159 mutt_enter_command ();
2161 if (option (OPTNEEDRESORT)) {
2162 unset_option (OPTNEEDRESORT);
2163 CHECK_MODE (IsHeader (extra));
2164 set_option (OPTNEEDRESORT);
2167 if (old_PagerIndexLines != PagerIndexLines) {
2169 mutt_menuDestroy (&pager_index);
2173 if (option (OPTWRAP) != old_smart_wrap ||
2174 option (OPTMARKERS) != old_markers) {
2175 if (flags & M_PAGER_RETWINCH) {
2177 rc = OP_REFORMAT_WINCH;
2181 /* count the real lines above */
2183 for (i = 0; i <= topline; i++) {
2184 if (!lineInfo[i].continuation)
2188 /* we need to restart the whole thing */
2189 for (i = 0; i < maxLine; i++) {
2190 lineInfo[i].offset = 0;
2191 lineInfo[i].type = -1;
2192 lineInfo[i].continuation = 0;
2193 lineInfo[i].chunks = 0;
2194 lineInfo[i].search_cnt = -1;
2195 lineInfo[i].quote = NULL;
2197 p_realloc(&(lineInfo[i].syntax), 1);
2198 if (SearchCompiled && lineInfo[i].search)
2199 p_delete(&(lineInfo[i].search));
2202 if (SearchCompiled) {
2203 regfree (&SearchRE);
2208 /* try to keep the old position */
2211 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2212 &lastLine, &maxLine,
2213 (has_types ? M_TYPES : 0),
2214 &QuoteList, &q_level, &force_redraw,
2216 redraw |= REDRAW_SIDEBAR;
2217 if (!lineInfo[topline].continuation)
2226 if (option (OPTFORCEREDRAWPAGER))
2227 redraw = REDRAW_FULL;
2228 unset_option (OPTFORCEREDRAWINDEX);
2229 unset_option (OPTFORCEREDRAWPAGER);
2232 case OP_FLAG_MESSAGE:
2233 CHECK_MODE (IsHeader (extra));
2236 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2238 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2239 redraw = REDRAW_STATUS | REDRAW_INDEX;
2240 if (option (OPTRESOLVE)) {
2242 rc = OP_MAIN_NEXT_UNDELETED;
2247 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2248 if (IsAttach (extra))
2249 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2251 mutt_pipe_message (extra->hdr);
2252 MAYBE_REDRAW (redraw);
2256 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2257 if (IsAttach (extra))
2258 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2260 mutt_print_message (extra->hdr);
2264 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2266 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2267 redraw = REDRAW_FULL;
2272 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2274 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2275 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2276 query_quadoption (OPT_TOMODERATED,
2278 ("Posting to this group not allowed, may be moderated. Continue?"))
2281 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2282 redraw = REDRAW_FULL;
2285 case OP_FORWARD_TO_GROUP:
2286 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2288 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2289 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2290 query_quadoption (OPT_TOMODERATED,
2292 ("Posting to this group not allowed, may be moderated. Continue?"))
2295 if (IsMsgAttach (extra))
2296 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2297 extra->idxlen, extra->bdy, SENDNEWS);
2299 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2301 redraw = REDRAW_FULL;
2305 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2308 if (IsMsgAttach (extra))
2309 followup_to = extra->bdy->hdr->env->followup_to;
2311 followup_to = extra->hdr->env->followup_to;
2313 if (!followup_to || m_strcasecmp(followup_to, "poster") ||
2314 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2315 _("Reply by mail as poster prefers?")) != M_YES) {
2316 if (extra->ctx && extra->ctx->magic == M_NNTP
2317 && !((NNTP_DATA *) extra->ctx->data)->allowed
2318 && query_quadoption (OPT_TOMODERATED,
2320 ("Posting to this group not allowed, may be moderated. Continue?"))
2323 if (IsMsgAttach (extra))
2324 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2325 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2327 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2328 extra->ctx, extra->hdr);
2329 redraw = REDRAW_FULL;
2335 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2337 if (IsMsgAttach (extra))
2338 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2339 extra->idxlen, extra->bdy, SENDREPLY);
2341 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2342 redraw = REDRAW_FULL;
2345 case OP_RECALL_MESSAGE:
2346 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2348 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2349 redraw = REDRAW_FULL;
2352 case OP_GROUP_REPLY:
2353 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2355 if (IsMsgAttach (extra))
2356 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2357 extra->idxlen, extra->bdy,
2358 SENDREPLY | SENDGROUPREPLY);
2360 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2362 redraw = REDRAW_FULL;
2366 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2368 if (IsMsgAttach (extra))
2369 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2370 extra->idxlen, extra->bdy,
2371 SENDREPLY | SENDLISTREPLY);
2373 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2375 redraw = REDRAW_FULL;
2378 case OP_FORWARD_MESSAGE:
2379 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2381 if (IsMsgAttach (extra))
2382 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2383 extra->idxlen, extra->bdy, 0);
2385 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2386 redraw = REDRAW_FULL;
2389 case OP_DECRYPT_SAVE:
2391 if (IsAttach (extra)) {
2392 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2397 case OP_COPY_MESSAGE:
2398 case OP_DECODE_SAVE:
2399 case OP_DECODE_COPY:
2400 case OP_DECRYPT_COPY:
2401 CHECK_MODE (IsHeader (extra));
2402 if (mutt_save_message (extra->hdr,
2403 (ch == OP_DECRYPT_SAVE) ||
2404 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2405 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2406 (ch == OP_DECRYPT_SAVE)
2407 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2408 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2409 || ch == OP_DECRYPT_SAVE)) {
2410 if (option (OPTRESOLVE)) {
2412 rc = OP_MAIN_NEXT_UNDELETED;
2415 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2417 MAYBE_REDRAW (redraw);
2420 case OP_SHELL_ESCAPE:
2421 mutt_shell_escape ();
2422 MAYBE_REDRAW (redraw);
2426 CHECK_MODE (IsHeader (extra));
2427 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2429 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2430 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2431 ? NULL : Context->last_tag);
2433 redraw = REDRAW_STATUS | REDRAW_INDEX;
2434 if (option (OPTRESOLVE)) {
2441 CHECK_MODE (IsHeader (extra));
2444 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2446 if (extra->hdr->read || extra->hdr->old)
2447 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2449 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2451 Context->msgnotreadyet = -1;
2452 redraw = REDRAW_STATUS | REDRAW_INDEX;
2453 if (option (OPTRESOLVE)) {
2455 rc = OP_MAIN_NEXT_UNDELETED;
2460 CHECK_MODE (IsHeader (extra));
2463 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2465 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2466 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2467 redraw = REDRAW_STATUS | REDRAW_INDEX;
2468 if (option (OPTRESOLVE)) {
2474 case OP_UNDELETE_THREAD:
2475 case OP_UNDELETE_SUBTHREAD:
2476 CHECK_MODE (IsHeader (extra));
2479 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2481 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2482 ch == OP_UNDELETE_THREAD ? 0 : 1)
2483 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2484 ch == OP_UNDELETE_THREAD ? 0 : 1);
2487 if (option (OPTRESOLVE)) {
2488 rc = (ch == OP_DELETE_THREAD) ?
2489 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2493 if (!option (OPTRESOLVE) && PagerIndexLines)
2494 redraw = REDRAW_FULL;
2496 redraw = REDRAW_STATUS | REDRAW_INDEX;
2505 if (option (OPTFORCEBUFFYCHECK))
2508 redraw |= REDRAW_SIDEBAR;
2511 case OP_VIEW_ATTACHMENTS:
2512 if (flags & M_PAGER_ATTACHMENT) {
2514 rc = OP_ATTACH_COLLAPSE;
2517 CHECK_MODE (IsHeader (extra));
2518 mutt_view_attachments (extra->hdr);
2519 if (extra->hdr->attach_del)
2520 Context->changed = 1;
2521 redraw = REDRAW_FULL;
2526 CHECK_MODE (IsHeader (extra));
2528 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2529 redraw = REDRAW_FULL;
2533 case OP_FORGET_PASSPHRASE:
2534 crypt_forget_passphrase ();
2537 case OP_EXTRACT_KEYS:
2538 CHECK_MODE (IsHeader (extra));
2539 crypt_extract_keys_from_messages (extra->hdr);
2540 redraw = REDRAW_FULL;
2543 case OP_SIDEBAR_SCROLL_UP:
2544 case OP_SIDEBAR_SCROLL_DOWN:
2545 case OP_SIDEBAR_NEXT:
2546 case OP_SIDEBAR_NEXT_NEW:
2547 case OP_SIDEBAR_PREV:
2548 case OP_SIDEBAR_PREV_NEW:
2549 sidebar_scroll (ch);
2558 if (IsHeader (extra)) {
2559 Context->msgnotreadyet = -1;
2564 OldHdr = extra->hdr;
2568 cleanup_quote (&QuoteList);
2570 for (i = 0; i < maxLine; i++) {
2571 p_delete(&(lineInfo[i].syntax));
2572 if (SearchCompiled && lineInfo[i].search)
2573 p_delete(&(lineInfo[i].search));
2575 if (SearchCompiled) {
2576 regfree (&SearchRE);
2579 p_delete(&lineInfo);
2581 mutt_menuDestroy (&pager_index);
2582 return (rc != -1 ? rc : 0);