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>
27 #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;
666 else if (m_strncmp("[-- ", buf, 4) == 0)
667 lineInfo[n].type = MT_COLOR_ATTACHMENT;
669 else if (check_attachment_marker ((char *) rawbuf) == 0)
670 lineInfo[n].type = MT_COLOR_ATTACHMENT;
672 else if (m_strcmp("-- \n", buf) == 0
673 || m_strcmp("-- \r\n", buf) == 0) {
676 lineInfo[n].type = MT_COLOR_SIGNATURE;
677 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
678 (lineInfo[i].type == MT_COLOR_NORMAL ||
679 lineInfo[i].type == MT_COLOR_QUOTED ||
680 lineInfo[i].type == MT_COLOR_HEADER)) {
682 if (lineInfo[i].chunks) {
683 lineInfo[i].chunks = 0;
684 p_realloc(&(lineInfo[n].syntax), 1);
686 lineInfo[i++].type = MT_COLOR_SIGNATURE;
689 else if (check_sig (buf, lineInfo, n - 1) == 0)
690 lineInfo[n].type = MT_COLOR_SIGNATURE;
691 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
692 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
693 if (smatch[0].rm_so > 0) {
696 /* hack to avoid making an extra copy of buf */
697 c = buf[smatch[0].rm_so];
698 buf[smatch[0].rm_so] = 0;
700 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
701 if (q_classify && lineInfo[n].quote == NULL)
702 lineInfo[n].quote = classify_quote (QuoteList,
703 buf + pmatch[0].rm_so,
705 pmatch[0].rm_so, force_redraw,
707 lineInfo[n].type = MT_COLOR_QUOTED;
710 lineInfo[n].type = MT_COLOR_NORMAL;
712 buf[smatch[0].rm_so] = c;
715 lineInfo[n].type = MT_COLOR_NORMAL;
718 if (q_classify && lineInfo[n].quote == NULL)
719 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
720 pmatch[0].rm_eo - pmatch[0].rm_so,
721 force_redraw, q_level);
722 lineInfo[n].type = MT_COLOR_QUOTED;
726 lineInfo[n].type = MT_COLOR_NORMAL;
729 if (lineInfo[n].type == MT_COLOR_NORMAL ||
730 lineInfo[n].type == MT_COLOR_QUOTED) {
734 lineInfo[n].chunks = 0;
741 color_line = ColorBodyList;
743 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
744 (offset ? REG_NOTBOL : 0)) == 0) {
745 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
747 if (++(lineInfo[n].chunks) > 1)
748 p_realloc(&(lineInfo[n].syntax), lineInfo[n].chunks);
750 i = lineInfo[n].chunks - 1;
751 pmatch[0].rm_so += offset;
752 pmatch[0].rm_eo += offset;
754 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
755 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
756 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
757 (lineInfo[n].syntax)[i].color = color_line->pair;
758 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
759 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
765 null_rx = 1; /* empty regexp; don't add it, but keep looking */
767 color_line = color_line->next;
771 offset++; /* avoid degenerate cases */
773 offset = (lineInfo[n].syntax)[i].last;
774 } while (found || null_rx);
778 static int is_ansi (unsigned char *buf)
780 while (*buf && (isdigit (*buf) || *buf == ';'))
782 return (*buf == 'm');
785 static int check_attachment_marker (char *p)
787 char *q = AttachmentMarker;
789 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
790 return (int) (*p - *q);
793 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
797 while (isdigit (buf[x]) || buf[x] == ';')
800 /* Character Attributes */
801 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
805 mutt_free_color (a->fg, a->bg);
813 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
814 a->attr |= ANSI_BOLD;
817 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
818 a->attr |= ANSI_UNDERLINE;
821 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
822 a->attr |= ANSI_BLINK;
825 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
826 a->attr |= ANSI_REVERSE;
829 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
834 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
837 mutt_free_color (a->fg, a->bg);
840 a->attr |= ANSI_COLOR;
841 if (buf[pos + 1] != '9')
842 a->fg = buf[pos + 1] - '0';
845 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
848 mutt_free_color (a->fg, a->bg);
851 a->attr |= ANSI_COLOR;
852 if (buf[pos + 1] != '9')
853 a->bg = buf[pos + 1] - '0';
857 while (pos < x && buf[pos] != ';')
867 /* trim tail of buf so that it contains complete multibyte characters */
868 static int trim_incomplete_mbyte(unsigned char *buf, size_t len) {
872 p_clear(&mbstate, 1);
873 for (; len > 0; buf += k, len -= k) {
874 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
877 else if (k == -1 || k == 0)
886 fill_buffer (FILE * f, off_t *last_pos, off_t offset, unsigned char *buf,
887 unsigned char *fmt, ssize_t blen, int *buf_ready)
890 static int b_read = 0;
892 if (*buf_ready == 0) {
894 if (offset != *last_pos)
895 fseeko (f, offset, 0);
896 if (fgets ((char *) buf, blen - 1, f) == NULL) {
900 *last_pos = ftello (f);
901 b_read = (int) (*last_pos - offset);
904 /* incomplete mbyte characters trigger a segfault in regex processing for
905 * certain versions of glibc. Trim them if necessary. */
906 if (b_read == blen - 2)
907 b_read -= trim_incomplete_mbyte(buf, b_read);
909 /* copy "buf" to "fmt", but without bold and underline controls */
912 if (*p == '\010' && (p > buf)) {
913 if (*(p + 1) == '_') /* underline */
915 else if (*(p + 1)) { /* bold or overstrike */
916 *(fmt - 1) = *(p + 1);
922 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
923 while (*p++ != 'm') /* skip ANSI sequence */
926 else if (*p == '\033' && *(p + 1) == ']'
927 && check_attachment_marker ((char *) p) == 0) {
928 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
945 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
946 int flags, ansi_attr * pa, int cnt,
947 int *pspace, int *pvch, int *pcol, int *pspecial)
949 int space = -1; /* index of the last space or TAB */
950 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
951 int ch, vch, k, last_special = -1, special = 0, t;
955 int wrap_cols = COLS;
957 if (!(flags & (M_SHOWFLAT)))
958 wrap_cols -= WrapMargin;
964 /* FIXME: this should come from lineInfo */
965 p_clear(&mbstate, 1);
967 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
968 /* Handle ANSI sequences */
969 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
970 is_ansi (buf + ch + 2))
971 ch = grok_ansi (buf, ch + 2, pa) + 1;
973 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
974 check_attachment_marker ((char *) buf + ch) == 0) {
975 while (buf[ch++] != '\a')
980 /* is anything left to do? */
984 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
985 if (k == -2 || k == -1) {
986 if (col + 4 > wrap_cols)
990 printw ("\\%03o", buf[ch]);
997 /* Handle backspace */
1004 while ((wc1 = 0, mbstate1 = mbstate,
1006 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
1007 &mbstate1), k1 - k > 0 && wc1 == '\b')
1009 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1010 &mbstate1), k2 > 0 && iswprint(wc1))) {
1012 special |= (wc == '_' && special & A_UNDERLINE)
1013 ? A_UNDERLINE : A_BOLD;
1015 else if (wc == '_' || wc1 == '_') {
1016 special |= A_UNDERLINE;
1017 wc = (wc1 == '_') ? wc : wc1;
1020 /* special = 0; / * overstrike: nothing to do! */
1030 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1031 special || last_special || pa->attr)) {
1032 resolve_color (*lineInfo, n, vch, flags, special, pa);
1033 last_special = special;
1040 if (col + t > wrap_cols)
1046 else if (wc == '\n')
1048 else if (wc == '\t') {
1054 for (; col < t; col++)
1059 else if (wc < 0x20 || wc == 0x7f) {
1060 if (col + 2 > wrap_cols)
1064 printw ("^%c", ('@' + wc) & 0x7f);
1066 else if (wc < 0x100) {
1067 if (col + 4 > wrap_cols)
1071 printw ("\\%03o", wc);
1074 if (col + 1 > wrap_cols)
1078 addch (CharsetReplacement);
1084 *pspecial = special;
1090 * flags M_SHOWFLAT, show characters (used for displaying help)
1091 * M_SHOWCOLOR, show characters in color
1092 * otherwise don't show characters
1093 * M_HIDE, don't show quoted text
1094 * M_SEARCH, resolve search patterns
1095 * M_TYPES, compute line's type
1096 * M_PAGER_NSKIP, keeps leading whitespace
1097 * M_PAGER_MARKER, eventually show markers
1100 * -1 EOF was reached
1101 * 0 normal exit, line was not displayed
1102 * >0 normal exit, line was displayed
1106 display_line (FILE * f, off_t *last_pos, struct line_t **lineInfo, int n,
1107 int *last, int *max, int flags, struct q_class_t **QuoteList,
1108 int *q_level, int *force_redraw, regex_t * SearchRE)
1110 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1111 unsigned char *buf_ptr = buf;
1112 int ch, vch, col, cnt, b_read;
1113 int buf_ready = 0, change_last = 0;
1118 ansi_attr a = { 0, 0, 0, -1 };
1119 regmatch_t pmatch[1];
1126 if (*last == *max) {
1127 p_realloc(lineInfo, *max += LINES);
1128 for (ch = *last; ch < *max; ch++) {
1129 p_clear(&(*lineInfo)[ch], 1);
1130 (*lineInfo)[ch].type = -1;
1131 (*lineInfo)[ch].search_cnt = -1;
1132 (*lineInfo)[ch].syntax = p_new(struct syntax_t, 1);
1133 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1138 /* only do color hiliting if we are viewing a message */
1139 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1140 if ((*lineInfo)[n].type == -1) {
1141 /* determine the line class */
1143 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1150 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1151 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1153 /* avoid race condition for continuation lines when scrolling up */
1155 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1157 (*lineInfo)[m].type = (*lineInfo)[n].type;
1160 /* this also prevents searching through the hidden lines */
1161 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1162 flags = 0; /* M_NOSHOW */
1165 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1166 * don't want to compute it every time M_TYPES is set, since this
1167 * would slow down the "bottom" function unacceptably. A compromise
1168 * solution is hence to call regexec() again, just to find out the
1169 * length of the quote prefix.
1171 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1172 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1175 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1181 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1182 (*lineInfo)[n].quote = classify_quote (QuoteList,
1183 (char *) fmt + pmatch[0].rm_so,
1184 pmatch[0].rm_eo - pmatch[0].rm_so,
1185 force_redraw, q_level);
1188 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1189 && (*lineInfo)[n].search_cnt == -1) {
1191 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1199 (*lineInfo)[n].search_cnt = 0;
1201 (SearchRE, (char *) fmt + offset, 1, pmatch,
1202 (offset ? REG_NOTBOL : 0)) == 0) {
1203 if (++((*lineInfo)[n].search_cnt) > 1)
1204 p_realloc(&(*lineInfo)[n].search, (*lineInfo)[n].search_cnt);
1206 (*lineInfo)[n].search = p_new(struct syntax_t, 1);
1207 pmatch[0].rm_so += offset;
1208 pmatch[0].rm_eo += offset;
1209 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1211 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1214 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1215 offset++; /* avoid degenerate cases */
1217 offset = pmatch[0].rm_eo;
1223 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1224 /* we've already scanned this line, so just exit */
1227 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1228 /* no need to try to display this line... */
1229 return (1); /* fake display */
1232 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1233 sizeof (buf), &buf_ready)) < 0) {
1239 /* now chose a good place to break the line */
1241 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1243 buf_ptr = buf + cnt;
1245 /* move the break point only if smart_wrap is set */
1246 if (option (OPTWRAP)) {
1248 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1249 && buf[cnt] != '\r') {
1251 /* skip trailing blanks */
1252 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1254 /* a very long word with leading spaces causes infinite wrapping */
1255 if ((!ch) && (flags & M_PAGER_NSKIP))
1256 buf_ptr = buf + cnt;
1261 buf_ptr = buf + cnt; /* a very long word... */
1263 if (!(flags & M_PAGER_NSKIP))
1264 /* skip leading blanks on the next line too */
1265 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1269 if (*buf_ptr == '\r')
1271 if (*buf_ptr == '\n')
1274 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1275 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1276 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1278 /* if we don't need to display the line we are done */
1279 if (!(flags & M_SHOW))
1282 /* display the line */
1283 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1285 /* avoid a bug in ncurses... */
1286 #ifndef USE_SLANG_CURSES
1288 SETCOLOR (MT_COLOR_NORMAL);
1293 /* end the last color pattern (needed by S-Lang) */
1294 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1295 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1298 * Fill the blank space at the end of the line with the prevailing color.
1299 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1300 * to make sure to reset the color *after* that
1302 if (flags & M_SHOWCOLOR) {
1303 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1304 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1305 def_color = ((*lineInfo)[m].syntax)[0].color;
1307 def_color = ColorDefs[(*lineInfo)[m].type];
1309 attrset (def_color);
1311 bkgdset (def_color | ' ');
1315 /* ncurses always wraps lines when you get to the right side of the
1316 * screen, but S-Lang seems to only wrap if the next character is *not*
1319 #ifndef USE_SLANG_CURSES
1325 * reset the color back to normal. This *must* come after the
1326 * addch('\n'), otherwise the color for this line will not be
1327 * filled to the right margin.
1329 if (flags & M_SHOWCOLOR) {
1330 SETCOLOR (MT_COLOR_NORMAL);
1331 BKGDSET (MT_COLOR_NORMAL);
1334 /* build a return code */
1335 if (!(flags & M_SHOW))
1341 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1343 while (cur > 0 && nlines > 0) {
1345 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1352 static struct mapping_t PagerHelp[] = {
1353 {N_("Exit"), OP_EXIT},
1354 {N_("PrevPg"), OP_PREV_PAGE},
1355 {N_("NextPg"), OP_NEXT_PAGE},
1358 static struct mapping_t PagerHelpExtra[] = {
1359 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1360 {N_("Del"), OP_DELETE},
1361 {N_("Reply"), OP_REPLY},
1362 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1367 static struct mapping_t PagerNewsHelpExtra[] = {
1368 {N_("Post"), OP_POST},
1369 {N_("Followup"), OP_FOLLOWUP},
1370 {N_("Del"), OP_DELETE},
1371 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1378 /* This pager is actually not so simple as it once was. It now operates in
1379 two modes: one for viewing messages and the other for viewing help. These
1380 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1381 is there so that we can do operations on the current message without the
1382 need to pop back out to the main-menu. */
1384 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1386 static char searchbuf[STRING];
1387 char buffer[LONG_STRING];
1388 char helpstr[SHORT_STRING * 2];
1389 char tmphelp[SHORT_STRING * 2];
1390 int maxLine, lastLine = 0;
1391 struct line_t *lineInfo;
1392 struct q_class_t *QuoteList = NULL;
1393 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1394 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1396 int redraw = REDRAW_FULL;
1398 off_t last_pos = 0, last_offset = 0;
1399 int old_smart_wrap, old_markers;
1402 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1403 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1405 int bodyoffset = 1; /* offset of first line of real text */
1406 int statusoffset = 0; /* offset for the status bar */
1407 int helpoffset = LINES - 2; /* offset for the help bar. */
1408 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1410 MUTTMENU *pager_index = NULL; /* the Pager Index (PI) */
1411 int indexoffset = 0; /* offset for the PI */
1412 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1413 int indicator = indexlen / 3; /* the indicator line of the PI */
1414 int old_PagerIndexLines; /* some people want to resize it
1415 * while inside the pager... */
1421 if (!(flags & M_SHOWCOLOR))
1422 flags |= M_SHOWFLAT;
1424 if ((fp = fopen (fname, "r")) == NULL) {
1425 mutt_perror (fname);
1429 if (stat (fname, &sb) != 0) {
1430 mutt_perror (fname);
1436 /* Initialize variables */
1438 if (IsHeader (extra) && !extra->hdr->read) {
1439 Context->msgnotreadyet = extra->hdr->msgno;
1440 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1443 lineInfo = p_new(struct line_t, maxLine = LINES);
1444 for (i = 0; i < maxLine; i++) {
1445 p_clear(&lineInfo[i], 1);
1446 lineInfo[i].type = -1;
1447 lineInfo[i].search_cnt = -1;
1448 lineInfo[i].syntax = p_new(struct syntax_t, 1);
1449 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1452 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1453 if (IsHeader (extra)) {
1454 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1455 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1458 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1461 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1464 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1465 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1466 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1473 if (redraw & REDRAW_FULL) {
1474 SETCOLOR (MT_COLOR_NORMAL);
1475 /* clear() doesn't optimize screen redraws */
1479 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1480 indexlen = Context->vcount + 1;
1482 indexlen = PagerIndexLines;
1484 indicator = indexlen / 3;
1486 if (option (OPTSTATUSONTOP)) {
1488 statusoffset = IsHeader (extra) ? indexlen : 0;
1489 bodyoffset = statusoffset + 1;
1490 helpoffset = LINES - 2;
1491 bodylen = helpoffset - bodyoffset;
1492 if (!option (OPTHELP))
1498 statusoffset = LINES - 2;
1499 if (!option (OPTHELP))
1501 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1502 bodylen = statusoffset - bodyoffset;
1505 if (option (OPTHELP)) {
1506 SETCOLOR (MT_COLOR_STATUS);
1507 move (helpoffset, SW);
1508 mutt_paddstr (COLS-SW, helpstr);
1509 SETCOLOR (MT_COLOR_NORMAL);
1512 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1513 if (Resize != NULL) {
1514 if ((SearchCompiled = Resize->SearchCompiled)) {
1516 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1517 SearchFlag = M_SEARCH;
1518 SearchBack = Resize->SearchBack;
1520 lines = Resize->line;
1521 redraw |= REDRAW_SIGWINCH;
1527 if (IsHeader (extra) && PagerIndexLines) {
1528 if (pager_index == NULL) {
1529 /* only allocate the space if/when we need the index.
1530 Initialise the menu as per the main index */
1531 pager_index = mutt_new_menu ();
1532 pager_index->menu = MENU_MAIN;
1533 pager_index->make_entry = index_make_entry;
1534 pager_index->color = index_color;
1535 pager_index->max = Context->vcount;
1536 pager_index->current = extra->hdr->virtual;
1539 SETCOLOR (MT_COLOR_NORMAL);
1540 pager_index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1542 pager_index->pagelen = indexlen - 1;
1544 /* some fudge to work out where abouts the indicator should go */
1545 if (pager_index->current - indicator < 0)
1546 pager_index->top = 0;
1547 else if (pager_index->max - pager_index->current < pager_index->pagelen - indicator)
1548 pager_index->top = pager_index->max - pager_index->pagelen;
1550 pager_index->top = pager_index->current - indicator;
1552 menu_redraw_index (pager_index);
1555 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1559 if (redraw & REDRAW_SIGWINCH) {
1562 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1563 has_types | SearchFlag, &QuoteList, &q_level,
1564 &force_redraw, &SearchRE) == 0) {
1565 if (!lineInfo[i].continuation && ++j == lines) {
1570 redraw |= REDRAW_SIDEBAR;
1574 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1576 move (bodyoffset, SW);
1577 curline = oldtopline = topline;
1581 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1582 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1584 (flags & M_DISPLAYFLAGS) | hideQuoted |
1585 SearchFlag, &QuoteList, &q_level, &force_redraw,
1589 move (lines + bodyoffset, SW);
1590 redraw |= REDRAW_SIDEBAR;
1592 last_offset = lineInfo[curline].offset;
1593 } while (force_redraw);
1595 SETCOLOR (MT_COLOR_TILDE);
1596 BKGDSET (MT_COLOR_TILDE);
1597 while (lines < bodylen) {
1599 if (option (OPTTILDE))
1603 move (lines + bodyoffset, SW);
1605 /* We are going to update the pager status bar, so it isn't
1606 * necessary to reset to normal color now. */
1608 redraw |= REDRAW_STATUS; /* need to update the % seen */
1611 if (redraw & REDRAW_STATUS) {
1612 /* print out the pager status bar */
1613 SETCOLOR (MT_COLOR_STATUS);
1614 BKGDSET (MT_COLOR_STATUS);
1615 CLEARLINE_WIN (statusoffset);
1616 if (IsHeader (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->hdr, M_FORMAT_MAKEPRINT);
1623 else if (IsMsgAttach (extra)) {
1624 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1625 size_t l2 = sizeof (buffer);
1627 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1628 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1630 move(statusoffset,SW);
1631 mutt_paddstr (COLS - 10 - SW, IsHeader (extra) ||
1632 IsMsgAttach (extra) ? buffer : banner);
1635 if (last_pos < sb.st_size - 1)
1636 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1638 addstr (topline == 0 ? "all)" : "end)");
1639 BKGDSET (MT_COLOR_NORMAL);
1640 SETCOLOR (MT_COLOR_NORMAL);
1643 if (redraw & REDRAW_SIDEBAR)
1646 if ((redraw & REDRAW_INDEX) && pager_index) {
1647 /* redraw the pager_index indicator, because the
1648 * flags for this message might have changed. */
1649 menu_redraw_current (pager_index);
1651 /* print out the pager_index status bar */
1652 menu_status_line (buffer, sizeof (buffer), pager_index, NONULL (Status));
1653 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), SW);
1654 SETCOLOR (MT_COLOR_STATUS);
1655 BKGDSET (MT_COLOR_STATUS);
1656 mutt_paddstr (COLS-SW, buffer);
1657 SETCOLOR (MT_COLOR_NORMAL);
1658 BKGDSET (MT_COLOR_NORMAL);
1660 /* if we're not using the index, update every time */
1666 if (option(OPTBRAILLEFRIENDLY)) {
1667 if (brailleLine!=-1) {
1668 move(brailleLine+1, 0);
1672 move (statusoffset, COLS-1);
1675 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1676 && lineInfo[curline].offset < sb.st_size-1) {
1677 if (TopLine - topline > lines)
1686 ch = km_dokey (MENU_PAGER);
1688 mutt_clear_error ();
1695 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1696 else if (SigWinch) {
1697 mutt_resize_screen ();
1699 /* Store current position. */
1701 for (i = 0; i <= topline; i++)
1702 if (!lineInfo[i].continuation)
1705 if (flags & M_PAGER_RETWINCH) {
1706 Resize = p_new(struct resize, 1);
1708 Resize->line = lines;
1709 Resize->SearchCompiled = SearchCompiled;
1710 Resize->SearchBack = SearchBack;
1713 rc = OP_REFORMAT_WINCH;
1716 for (i = 0; i < maxLine; i++) {
1717 lineInfo[i].offset = 0;
1718 lineInfo[i].type = -1;
1719 lineInfo[i].continuation = 0;
1720 lineInfo[i].chunks = 0;
1721 lineInfo[i].search_cnt = -1;
1722 lineInfo[i].quote = NULL;
1724 p_realloc(&lineInfo[i].syntax, 1);
1725 if (SearchCompiled && lineInfo[i].search)
1726 p_delete(&(lineInfo[i].search));
1732 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1737 clearok (stdscr, TRUE); /*force complete redraw */
1741 else if (ch == -1) {
1755 if (lineInfo[curline].offset < sb.st_size - 1) {
1756 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1758 else if (option (OPTPAGERSTOP)) {
1759 /* emulate "less -q" and don't go on to the next message. */
1760 mutt_error _("Bottom of message is shown.");
1763 /* end of the current message, so display the next message. */
1764 rc = OP_MAIN_NEXT_UNDELETED;
1772 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1775 mutt_error _("Top of message is shown.");
1779 if (lineInfo[curline].offset < sb.st_size - 1) {
1782 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1788 mutt_error _("Bottom of message is shown.");
1793 topline = upNLines (1, lineInfo, topline, hideQuoted);
1795 mutt_error _("Top of message is shown.");
1802 mutt_error _("Top of message is shown.");
1807 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1809 mutt_error _("Top of message is shown.");
1813 if (lineInfo[curline].offset < sb.st_size - 1) {
1814 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1816 else if (option (OPTPAGERSTOP)) {
1817 /* emulate "less -q" and don't go on to the next message. */
1818 mutt_error _("Bottom of message is shown.");
1821 /* end of the current message, so display the next message. */
1822 rc = OP_MAIN_NEXT_UNDELETED;
1827 case OP_SEARCH_NEXT:
1828 case OP_SEARCH_OPPOSITE:
1829 if (SearchCompiled) {
1831 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1832 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1833 /* searching forward */
1834 for (i = topline + 1; i < lastLine; i++) {
1835 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1836 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1843 mutt_error _("Not found.");
1846 /* searching backward */
1847 for (i = topline - 1; i >= 0; i--) {
1848 if ((!hideQuoted || (has_types &&
1849 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1850 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1857 mutt_error _("Not found.");
1860 if (lineInfo[topline].search_cnt > 0)
1861 SearchFlag = M_SEARCH;
1865 /* no previous search pattern, so fall through to search */
1868 case OP_SEARCH_REVERSE:
1869 m_strcpy(buffer, sizeof(buffer), searchbuf);
1870 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1871 _("Search: ")), buffer, sizeof (buffer),
1875 if (!strcmp (buffer, searchbuf)) {
1876 if (SearchCompiled) {
1877 /* do an implicit search-next */
1878 if (ch == OP_SEARCH)
1879 ch = OP_SEARCH_NEXT;
1881 ch = OP_SEARCH_OPPOSITE;
1890 m_strcpy(searchbuf, sizeof(searchbuf), buffer);
1892 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1893 if (ch == OP_SEARCH)
1895 else if (ch == OP_SEARCH_REVERSE)
1898 if (SearchCompiled) {
1899 regfree (&SearchRE);
1900 for (i = 0; i < lastLine; i++) {
1901 if (lineInfo[i].search)
1902 p_delete(&(lineInfo[i].search));
1903 lineInfo[i].search_cnt = -1;
1908 REGCOMP (&SearchRE, searchbuf,
1909 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1910 regerror (err, &SearchRE, buffer, sizeof (buffer));
1911 mutt_error ("%s", buffer);
1912 regfree (&SearchRE);
1913 for (i = 0; i < maxLine; i++) {
1915 if (lineInfo[i].search)
1916 p_delete(&(lineInfo[i].search));
1917 lineInfo[i].search_cnt = -1;
1924 /* update the search pointers */
1926 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1927 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1928 &QuoteList, &q_level,
1929 &force_redraw, &SearchRE) == 0) {
1931 redraw |= REDRAW_SIDEBAR;
1935 /* searching forward */
1936 for (i = topline; i < lastLine; i++) {
1937 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1938 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1946 /* searching backward */
1947 for (i = topline; i >= 0; i--) {
1948 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1949 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1957 if (lineInfo[topline].search_cnt == 0) {
1959 mutt_error _("Not found.");
1962 SearchFlag = M_SEARCH;
1964 redraw = REDRAW_BODY;
1967 case OP_SEARCH_TOGGLE:
1968 if (SearchCompiled) {
1969 SearchFlag ^= M_SEARCH;
1970 redraw = REDRAW_BODY;
1975 /* don't let the user enter the help-menu from the help screen! */
1978 mutt_help (MENU_PAGER);
1979 redraw = REDRAW_FULL;
1983 mutt_error _("Help is currently being shown.");
1986 case OP_PAGER_HIDE_QUOTED:
1988 hideQuoted ^= M_HIDE;
1989 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1990 topline = upNLines (1, lineInfo, topline, hideQuoted);
1992 redraw = REDRAW_BODY;
1996 case OP_PAGER_SKIP_QUOTED:
1999 int new_topline = topline;
2001 while ((new_topline < lastLine ||
2002 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2003 new_topline, &lastLine,
2004 &maxLine, M_TYPES, &QuoteList,
2005 &q_level, &force_redraw,
2007 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
2008 redraw |= REDRAW_SIDEBAR;
2013 mutt_error _("No more quoted text.");
2018 while ((new_topline < lastLine ||
2019 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2020 new_topline, &lastLine,
2021 &maxLine, M_TYPES, &QuoteList,
2022 &q_level, &force_redraw,
2024 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2026 redraw |= REDRAW_SIDEBAR;
2030 mutt_error _("No more unquoted text after quoted text.");
2034 topline = new_topline;
2038 case OP_PAGER_BOTTOM: /* move to the end of the file */
2039 if (lineInfo[curline].offset < sb.st_size - 1) {
2041 /* make sure the types are defined to the end of file */
2042 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2043 &maxLine, has_types,
2044 &QuoteList, &q_level, &force_redraw,
2047 redraw |= REDRAW_SIDEBAR;
2049 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2052 mutt_error _("Bottom of message is shown.");
2056 clearok (stdscr, TRUE);
2057 redraw = REDRAW_FULL;
2061 km_error_key (MENU_PAGER);
2064 /* --------------------------------------------------------------------
2065 * The following are operations on the current message rather than
2066 * adjusting the view of the message.
2069 case OP_BOUNCE_MESSAGE:
2070 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2072 if (IsMsgAttach (extra))
2073 mutt_attach_bounce (extra->fp, extra->hdr,
2074 extra->idx, extra->idxlen, extra->bdy);
2076 ci_bounce_message (extra->hdr, &redraw);
2080 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2082 if (IsMsgAttach (extra))
2083 mutt_attach_resend (extra->fp, extra->hdr,
2084 extra->idx, extra->idxlen, extra->bdy);
2086 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2087 redraw = REDRAW_FULL;
2090 case OP_CHECK_TRADITIONAL:
2091 CHECK_MODE (IsHeader (extra));
2092 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2094 rc = OP_CHECK_TRADITIONAL;
2098 case OP_CREATE_ALIAS:
2099 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2100 if (IsMsgAttach (extra))
2101 mutt_create_alias (extra->bdy->hdr->env, NULL);
2103 mutt_create_alias (extra->hdr->env, NULL);
2104 MAYBE_REDRAW (redraw);
2107 case OP_PURGE_MESSAGE:
2109 CHECK_MODE (IsHeader (extra));
2112 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2114 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2115 mutt_set_flag (Context, extra->hdr, M_PURGED,
2116 ch != OP_PURGE_MESSAGE ? 0 : 1);
2117 if (option (OPTDELETEUNTAG))
2118 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2119 redraw = REDRAW_STATUS | REDRAW_INDEX;
2120 if (option (OPTRESOLVE)) {
2122 rc = OP_MAIN_NEXT_UNDELETED;
2126 case OP_DELETE_THREAD:
2127 case OP_DELETE_SUBTHREAD:
2128 CHECK_MODE (IsHeader (extra));
2131 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2133 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2134 ch == OP_DELETE_THREAD ? 0 : 1);
2137 if (option (OPTDELETEUNTAG))
2138 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2139 ch == OP_DELETE_THREAD ? 0 : 1);
2140 if (option (OPTRESOLVE)) {
2141 rc = OP_MAIN_NEXT_UNDELETED;
2145 if (!option (OPTRESOLVE) && PagerIndexLines)
2146 redraw = REDRAW_FULL;
2148 redraw = REDRAW_STATUS | REDRAW_INDEX;
2152 case OP_DISPLAY_ADDRESS:
2153 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2154 if (IsMsgAttach (extra))
2155 mutt_display_address (extra->bdy->hdr->env);
2157 mutt_display_address (extra->hdr->env);
2160 case OP_ENTER_COMMAND:
2161 old_smart_wrap = option (OPTWRAP);
2162 old_markers = option (OPTMARKERS);
2163 old_PagerIndexLines = PagerIndexLines;
2165 CurrentMenu = MENU_PAGER;
2166 mutt_enter_command ();
2168 if (option (OPTNEEDRESORT)) {
2169 unset_option (OPTNEEDRESORT);
2170 CHECK_MODE (IsHeader (extra));
2171 set_option (OPTNEEDRESORT);
2174 if (old_PagerIndexLines != PagerIndexLines) {
2176 mutt_menuDestroy (&pager_index);
2180 if (option (OPTWRAP) != old_smart_wrap ||
2181 option (OPTMARKERS) != old_markers) {
2182 if (flags & M_PAGER_RETWINCH) {
2184 rc = OP_REFORMAT_WINCH;
2188 /* count the real lines above */
2190 for (i = 0; i <= topline; i++) {
2191 if (!lineInfo[i].continuation)
2195 /* we need to restart the whole thing */
2196 for (i = 0; i < maxLine; i++) {
2197 lineInfo[i].offset = 0;
2198 lineInfo[i].type = -1;
2199 lineInfo[i].continuation = 0;
2200 lineInfo[i].chunks = 0;
2201 lineInfo[i].search_cnt = -1;
2202 lineInfo[i].quote = NULL;
2204 p_realloc(&(lineInfo[i].syntax), 1);
2205 if (SearchCompiled && lineInfo[i].search)
2206 p_delete(&(lineInfo[i].search));
2209 if (SearchCompiled) {
2210 regfree (&SearchRE);
2215 /* try to keep the old position */
2218 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2219 &lastLine, &maxLine,
2220 (has_types ? M_TYPES : 0),
2221 &QuoteList, &q_level, &force_redraw,
2223 redraw |= REDRAW_SIDEBAR;
2224 if (!lineInfo[topline].continuation)
2233 if (option (OPTFORCEREDRAWPAGER))
2234 redraw = REDRAW_FULL;
2235 unset_option (OPTFORCEREDRAWINDEX);
2236 unset_option (OPTFORCEREDRAWPAGER);
2239 case OP_FLAG_MESSAGE:
2240 CHECK_MODE (IsHeader (extra));
2243 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2245 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2246 redraw = REDRAW_STATUS | REDRAW_INDEX;
2247 if (option (OPTRESOLVE)) {
2249 rc = OP_MAIN_NEXT_UNDELETED;
2254 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2255 if (IsAttach (extra))
2256 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2258 mutt_pipe_message (extra->hdr);
2259 MAYBE_REDRAW (redraw);
2263 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2264 if (IsAttach (extra))
2265 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2267 mutt_print_message (extra->hdr);
2271 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2273 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2274 redraw = REDRAW_FULL;
2279 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2281 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2282 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2283 query_quadoption (OPT_TOMODERATED,
2285 ("Posting to this group not allowed, may be moderated. Continue?"))
2288 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2289 redraw = REDRAW_FULL;
2292 case OP_FORWARD_TO_GROUP:
2293 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2295 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2296 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2297 query_quadoption (OPT_TOMODERATED,
2299 ("Posting to this group not allowed, may be moderated. Continue?"))
2302 if (IsMsgAttach (extra))
2303 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2304 extra->idxlen, extra->bdy, SENDNEWS);
2306 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2308 redraw = REDRAW_FULL;
2312 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2315 if (IsMsgAttach (extra))
2316 followup_to = extra->bdy->hdr->env->followup_to;
2318 followup_to = extra->hdr->env->followup_to;
2320 if (!followup_to || m_strcasecmp(followup_to, "poster") ||
2321 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2322 _("Reply by mail as poster prefers?")) != M_YES) {
2323 if (extra->ctx && extra->ctx->magic == M_NNTP
2324 && !((NNTP_DATA *) extra->ctx->data)->allowed
2325 && query_quadoption (OPT_TOMODERATED,
2327 ("Posting to this group not allowed, may be moderated. Continue?"))
2330 if (IsMsgAttach (extra))
2331 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2332 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2334 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2335 extra->ctx, extra->hdr);
2336 redraw = REDRAW_FULL;
2342 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2344 if (IsMsgAttach (extra))
2345 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2346 extra->idxlen, extra->bdy, SENDREPLY);
2348 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2349 redraw = REDRAW_FULL;
2352 case OP_RECALL_MESSAGE:
2353 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2355 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2356 redraw = REDRAW_FULL;
2359 case OP_GROUP_REPLY:
2360 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2362 if (IsMsgAttach (extra))
2363 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2364 extra->idxlen, extra->bdy,
2365 SENDREPLY | SENDGROUPREPLY);
2367 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2369 redraw = REDRAW_FULL;
2373 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2375 if (IsMsgAttach (extra))
2376 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2377 extra->idxlen, extra->bdy,
2378 SENDREPLY | SENDLISTREPLY);
2380 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2382 redraw = REDRAW_FULL;
2385 case OP_FORWARD_MESSAGE:
2386 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2388 if (IsMsgAttach (extra))
2389 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2390 extra->idxlen, extra->bdy, 0);
2392 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2393 redraw = REDRAW_FULL;
2396 case OP_DECRYPT_SAVE:
2398 if (IsAttach (extra)) {
2399 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2404 case OP_COPY_MESSAGE:
2405 case OP_DECODE_SAVE:
2406 case OP_DECODE_COPY:
2407 case OP_DECRYPT_COPY:
2408 CHECK_MODE (IsHeader (extra));
2409 if (mutt_save_message (extra->hdr,
2410 (ch == OP_DECRYPT_SAVE) ||
2411 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2412 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2413 (ch == OP_DECRYPT_SAVE)
2414 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2415 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2416 || ch == OP_DECRYPT_SAVE)) {
2417 if (option (OPTRESOLVE)) {
2419 rc = OP_MAIN_NEXT_UNDELETED;
2422 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2424 MAYBE_REDRAW (redraw);
2427 case OP_SHELL_ESCAPE:
2428 mutt_shell_escape ();
2429 MAYBE_REDRAW (redraw);
2433 CHECK_MODE (IsHeader (extra));
2434 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2436 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2437 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2438 ? NULL : Context->last_tag);
2440 redraw = REDRAW_STATUS | REDRAW_INDEX;
2441 if (option (OPTRESOLVE)) {
2448 CHECK_MODE (IsHeader (extra));
2451 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2453 if (extra->hdr->read || extra->hdr->old)
2454 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2456 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2458 Context->msgnotreadyet = -1;
2459 redraw = REDRAW_STATUS | REDRAW_INDEX;
2460 if (option (OPTRESOLVE)) {
2462 rc = OP_MAIN_NEXT_UNDELETED;
2467 CHECK_MODE (IsHeader (extra));
2470 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2472 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2473 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2474 redraw = REDRAW_STATUS | REDRAW_INDEX;
2475 if (option (OPTRESOLVE)) {
2481 case OP_UNDELETE_THREAD:
2482 case OP_UNDELETE_SUBTHREAD:
2483 CHECK_MODE (IsHeader (extra));
2486 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2488 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2489 ch == OP_UNDELETE_THREAD ? 0 : 1)
2490 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2491 ch == OP_UNDELETE_THREAD ? 0 : 1);
2494 if (option (OPTRESOLVE)) {
2495 rc = (ch == OP_DELETE_THREAD) ?
2496 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2500 if (!option (OPTRESOLVE) && PagerIndexLines)
2501 redraw = REDRAW_FULL;
2503 redraw = REDRAW_STATUS | REDRAW_INDEX;
2512 if (option (OPTFORCEBUFFYCHECK))
2515 redraw |= REDRAW_SIDEBAR;
2518 case OP_VIEW_ATTACHMENTS:
2519 if (flags & M_PAGER_ATTACHMENT) {
2521 rc = OP_ATTACH_COLLAPSE;
2524 CHECK_MODE (IsHeader (extra));
2525 mutt_view_attachments (extra->hdr);
2526 if (extra->hdr->attach_del)
2527 Context->changed = 1;
2528 redraw = REDRAW_FULL;
2533 CHECK_MODE (IsHeader (extra));
2535 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2536 redraw = REDRAW_FULL;
2540 case OP_FORGET_PASSPHRASE:
2541 crypt_forget_passphrase ();
2544 case OP_EXTRACT_KEYS:
2545 CHECK_MODE (IsHeader (extra));
2546 crypt_extract_keys_from_messages (extra->hdr);
2547 redraw = REDRAW_FULL;
2550 case OP_SIDEBAR_SCROLL_UP:
2551 case OP_SIDEBAR_SCROLL_DOWN:
2552 case OP_SIDEBAR_NEXT:
2553 case OP_SIDEBAR_NEXT_NEW:
2554 case OP_SIDEBAR_PREV:
2555 case OP_SIDEBAR_PREV_NEW:
2556 sidebar_scroll (ch);
2565 if (IsHeader (extra)) {
2566 Context->msgnotreadyet = -1;
2571 OldHdr = extra->hdr;
2575 cleanup_quote (&QuoteList);
2577 for (i = 0; i < maxLine; i++) {
2578 p_delete(&(lineInfo[i].syntax));
2579 if (SearchCompiled && lineInfo[i].search)
2580 p_delete(&(lineInfo[i].search));
2582 if (SearchCompiled) {
2583 regfree (&SearchRE);
2586 p_delete(&lineInfo);
2588 mutt_menuDestroy (&pager_index);
2589 return (rc != -1 ? rc : 0);