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;
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 */
940 #include <nntp/nntp.h>
943 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
944 int flags, ansi_attr * pa, int cnt,
945 int *pspace, int *pvch, int *pcol, int *pspecial)
947 int space = -1; /* index of the last space or TAB */
948 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
949 int ch, vch, k, last_special = -1, special = 0, t;
953 int wrap_cols = COLS;
955 if (!(flags & (M_SHOWFLAT)))
956 wrap_cols -= WrapMargin;
962 /* FIXME: this should come from lineInfo */
963 p_clear(&mbstate, 1);
965 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
966 /* Handle ANSI sequences */
967 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
968 is_ansi (buf + ch + 2))
969 ch = grok_ansi (buf, ch + 2, pa) + 1;
971 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
972 check_attachment_marker ((char *) buf + ch) == 0) {
973 while (buf[ch++] != '\a')
978 /* is anything left to do? */
982 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
983 if (k == -2 || k == -1) {
984 if (col + 4 > wrap_cols)
988 printw ("\\%03o", buf[ch]);
995 /* Handle backspace */
1002 while ((wc1 = 0, mbstate1 = mbstate,
1004 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
1005 &mbstate1), k1 - k > 0 && wc1 == '\b')
1007 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1008 &mbstate1), k2 > 0 && iswprint(wc1))) {
1010 special |= (wc == '_' && special & A_UNDERLINE)
1011 ? A_UNDERLINE : A_BOLD;
1013 else if (wc == '_' || wc1 == '_') {
1014 special |= A_UNDERLINE;
1015 wc = (wc1 == '_') ? wc : wc1;
1018 /* special = 0; / * overstrike: nothing to do! */
1028 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1029 special || last_special || pa->attr)) {
1030 resolve_color (*lineInfo, n, vch, flags, special, pa);
1031 last_special = special;
1038 if (col + t > wrap_cols)
1044 else if (wc == '\n')
1046 else if (wc == '\t') {
1052 for (; col < t; col++)
1057 else if (wc < 0x20 || wc == 0x7f) {
1058 if (col + 2 > wrap_cols)
1062 printw ("^%c", ('@' + wc) & 0x7f);
1064 else if (wc < 0x100) {
1065 if (col + 4 > wrap_cols)
1069 printw ("\\%03o", wc);
1072 if (col + 1 > wrap_cols)
1076 addch (CharsetReplacement);
1082 *pspecial = special;
1088 * flags M_SHOWFLAT, show characters (used for displaying help)
1089 * M_SHOWCOLOR, show characters in color
1090 * otherwise don't show characters
1091 * M_HIDE, don't show quoted text
1092 * M_SEARCH, resolve search patterns
1093 * M_TYPES, compute line's type
1094 * M_PAGER_NSKIP, keeps leading whitespace
1095 * M_PAGER_MARKER, eventually show markers
1098 * -1 EOF was reached
1099 * 0 normal exit, line was not displayed
1100 * >0 normal exit, line was displayed
1104 display_line (FILE * f, off_t *last_pos, struct line_t **lineInfo, int n,
1105 int *last, int *max, int flags, struct q_class_t **QuoteList,
1106 int *q_level, int *force_redraw, regex_t * SearchRE)
1108 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1109 unsigned char *buf_ptr = buf;
1110 int ch, vch, col, cnt, b_read;
1111 int buf_ready = 0, change_last = 0;
1116 ansi_attr a = { 0, 0, 0, -1 };
1117 regmatch_t pmatch[1];
1124 if (*last == *max) {
1125 p_realloc(lineInfo, *max += LINES);
1126 for (ch = *last; ch < *max; ch++) {
1127 p_clear(&(*lineInfo)[ch], 1);
1128 (*lineInfo)[ch].type = -1;
1129 (*lineInfo)[ch].search_cnt = -1;
1130 (*lineInfo)[ch].syntax = p_new(struct syntax_t, 1);
1131 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1136 /* only do color hiliting if we are viewing a message */
1137 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1138 if ((*lineInfo)[n].type == -1) {
1139 /* determine the line class */
1141 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1148 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1149 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1151 /* avoid race condition for continuation lines when scrolling up */
1153 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1155 (*lineInfo)[m].type = (*lineInfo)[n].type;
1158 /* this also prevents searching through the hidden lines */
1159 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1160 flags = 0; /* M_NOSHOW */
1163 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1164 * don't want to compute it every time M_TYPES is set, since this
1165 * would slow down the "bottom" function unacceptably. A compromise
1166 * solution is hence to call regexec() again, just to find out the
1167 * length of the quote prefix.
1169 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1170 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1173 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1179 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1180 (*lineInfo)[n].quote = classify_quote (QuoteList,
1181 (char *) fmt + pmatch[0].rm_so,
1182 pmatch[0].rm_eo - pmatch[0].rm_so,
1183 force_redraw, q_level);
1186 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1187 && (*lineInfo)[n].search_cnt == -1) {
1189 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1197 (*lineInfo)[n].search_cnt = 0;
1199 (SearchRE, (char *) fmt + offset, 1, pmatch,
1200 (offset ? REG_NOTBOL : 0)) == 0) {
1201 if (++((*lineInfo)[n].search_cnt) > 1)
1202 p_realloc(&(*lineInfo)[n].search, (*lineInfo)[n].search_cnt);
1204 (*lineInfo)[n].search = p_new(struct syntax_t, 1);
1205 pmatch[0].rm_so += offset;
1206 pmatch[0].rm_eo += offset;
1207 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1209 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1212 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1213 offset++; /* avoid degenerate cases */
1215 offset = pmatch[0].rm_eo;
1221 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1222 /* we've already scanned this line, so just exit */
1225 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1226 /* no need to try to display this line... */
1227 return (1); /* fake display */
1230 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1231 sizeof (buf), &buf_ready)) < 0) {
1237 /* now chose a good place to break the line */
1239 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1241 buf_ptr = buf + cnt;
1243 /* move the break point only if smart_wrap is set */
1244 if (option (OPTWRAP)) {
1246 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1247 && buf[cnt] != '\r') {
1249 /* skip trailing blanks */
1250 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1252 /* a very long word with leading spaces causes infinite wrapping */
1253 if ((!ch) && (flags & M_PAGER_NSKIP))
1254 buf_ptr = buf + cnt;
1259 buf_ptr = buf + cnt; /* a very long word... */
1261 if (!(flags & M_PAGER_NSKIP))
1262 /* skip leading blanks on the next line too */
1263 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1267 if (*buf_ptr == '\r')
1269 if (*buf_ptr == '\n')
1272 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1273 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1274 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1276 /* if we don't need to display the line we are done */
1277 if (!(flags & M_SHOW))
1280 /* display the line */
1281 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1283 /* avoid a bug in ncurses... */
1284 #ifndef USE_SLANG_CURSES
1286 SETCOLOR (MT_COLOR_NORMAL);
1291 /* end the last color pattern (needed by S-Lang) */
1292 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1293 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1296 * Fill the blank space at the end of the line with the prevailing color.
1297 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1298 * to make sure to reset the color *after* that
1300 if (flags & M_SHOWCOLOR) {
1301 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1302 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1303 def_color = ((*lineInfo)[m].syntax)[0].color;
1305 def_color = ColorDefs[(*lineInfo)[m].type];
1307 attrset (def_color);
1309 bkgdset (def_color | ' ');
1313 /* ncurses always wraps lines when you get to the right side of the
1314 * screen, but S-Lang seems to only wrap if the next character is *not*
1317 #ifndef USE_SLANG_CURSES
1323 * reset the color back to normal. This *must* come after the
1324 * addch('\n'), otherwise the color for this line will not be
1325 * filled to the right margin.
1327 if (flags & M_SHOWCOLOR) {
1328 SETCOLOR (MT_COLOR_NORMAL);
1329 BKGDSET (MT_COLOR_NORMAL);
1332 /* build a return code */
1333 if (!(flags & M_SHOW))
1339 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1341 while (cur > 0 && nlines > 0) {
1343 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1350 static struct mapping_t PagerHelp[] = {
1351 {N_("Exit"), OP_EXIT},
1352 {N_("PrevPg"), OP_PREV_PAGE},
1353 {N_("NextPg"), OP_NEXT_PAGE},
1356 static struct mapping_t PagerHelpExtra[] = {
1357 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1358 {N_("Del"), OP_DELETE},
1359 {N_("Reply"), OP_REPLY},
1360 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1365 static struct mapping_t PagerNewsHelpExtra[] = {
1366 {N_("Post"), OP_POST},
1367 {N_("Followup"), OP_FOLLOWUP},
1368 {N_("Del"), OP_DELETE},
1369 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1376 /* This pager is actually not so simple as it once was. It now operates in
1377 two modes: one for viewing messages and the other for viewing help. These
1378 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1379 is there so that we can do operations on the current message without the
1380 need to pop back out to the main-menu. */
1382 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1384 static char searchbuf[STRING];
1385 char buffer[LONG_STRING];
1386 char helpstr[SHORT_STRING * 2];
1387 char tmphelp[SHORT_STRING * 2];
1388 int maxLine, lastLine = 0;
1389 struct line_t *lineInfo;
1390 struct q_class_t *QuoteList = NULL;
1391 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1392 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1394 int redraw = REDRAW_FULL;
1396 off_t last_pos = 0, last_offset = 0;
1397 int old_smart_wrap, old_markers;
1400 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1401 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1403 int bodyoffset = 1; /* offset of first line of real text */
1404 int statusoffset = 0; /* offset for the status bar */
1405 int helpoffset = LINES - 2; /* offset for the help bar. */
1406 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1408 MUTTMENU *pager_index = NULL; /* the Pager Index (PI) */
1409 int indexoffset = 0; /* offset for the PI */
1410 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1411 int indicator = indexlen / 3; /* the indicator line of the PI */
1412 int old_PagerIndexLines; /* some people want to resize it
1413 * while inside the pager... */
1419 if (!(flags & M_SHOWCOLOR))
1420 flags |= M_SHOWFLAT;
1422 if ((fp = fopen (fname, "r")) == NULL) {
1423 mutt_perror (fname);
1427 if (stat (fname, &sb) != 0) {
1428 mutt_perror (fname);
1434 /* Initialize variables */
1436 if (IsHeader (extra) && !extra->hdr->read) {
1437 Context->msgnotreadyet = extra->hdr->msgno;
1438 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1441 lineInfo = p_new(struct line_t, maxLine = LINES);
1442 for (i = 0; i < maxLine; i++) {
1443 p_clear(&lineInfo[i], 1);
1444 lineInfo[i].type = -1;
1445 lineInfo[i].search_cnt = -1;
1446 lineInfo[i].syntax = p_new(struct syntax_t, 1);
1447 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1450 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1451 if (IsHeader (extra)) {
1452 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1453 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1456 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1459 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1462 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1463 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1464 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1471 if (redraw & REDRAW_FULL) {
1472 SETCOLOR (MT_COLOR_NORMAL);
1473 /* clear() doesn't optimize screen redraws */
1477 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1478 indexlen = Context->vcount + 1;
1480 indexlen = PagerIndexLines;
1482 indicator = indexlen / 3;
1484 if (option (OPTSTATUSONTOP)) {
1486 statusoffset = IsHeader (extra) ? indexlen : 0;
1487 bodyoffset = statusoffset + 1;
1488 helpoffset = LINES - 2;
1489 bodylen = helpoffset - bodyoffset;
1490 if (!option (OPTHELP))
1496 statusoffset = LINES - 2;
1497 if (!option (OPTHELP))
1499 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1500 bodylen = statusoffset - bodyoffset;
1503 if (option (OPTHELP)) {
1504 SETCOLOR (MT_COLOR_STATUS);
1505 move (helpoffset, SW);
1506 mutt_paddstr (COLS-SW, helpstr);
1507 SETCOLOR (MT_COLOR_NORMAL);
1510 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1511 if (Resize != NULL) {
1512 if ((SearchCompiled = Resize->SearchCompiled)) {
1514 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1515 SearchFlag = M_SEARCH;
1516 SearchBack = Resize->SearchBack;
1518 lines = Resize->line;
1519 redraw |= REDRAW_SIGWINCH;
1525 if (IsHeader (extra) && PagerIndexLines) {
1526 if (pager_index == NULL) {
1527 /* only allocate the space if/when we need the index.
1528 Initialise the menu as per the main index */
1529 pager_index = mutt_new_menu ();
1530 pager_index->menu = MENU_MAIN;
1531 pager_index->make_entry = index_make_entry;
1532 pager_index->color = index_color;
1533 pager_index->max = Context->vcount;
1534 pager_index->current = extra->hdr->virtual;
1537 SETCOLOR (MT_COLOR_NORMAL);
1538 pager_index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1540 pager_index->pagelen = indexlen - 1;
1542 /* some fudge to work out where abouts the indicator should go */
1543 if (pager_index->current - indicator < 0)
1544 pager_index->top = 0;
1545 else if (pager_index->max - pager_index->current < pager_index->pagelen - indicator)
1546 pager_index->top = pager_index->max - pager_index->pagelen;
1548 pager_index->top = pager_index->current - indicator;
1550 menu_redraw_index (pager_index);
1553 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1557 if (redraw & REDRAW_SIGWINCH) {
1560 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1561 has_types | SearchFlag, &QuoteList, &q_level,
1562 &force_redraw, &SearchRE) == 0) {
1563 if (!lineInfo[i].continuation && ++j == lines) {
1568 redraw |= REDRAW_SIDEBAR;
1572 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1574 move (bodyoffset, SW);
1575 curline = oldtopline = topline;
1579 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1580 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1582 (flags & M_DISPLAYFLAGS) | hideQuoted |
1583 SearchFlag, &QuoteList, &q_level, &force_redraw,
1587 move (lines + bodyoffset, SW);
1588 redraw |= REDRAW_SIDEBAR;
1590 last_offset = lineInfo[curline].offset;
1591 } while (force_redraw);
1593 SETCOLOR (MT_COLOR_TILDE);
1594 BKGDSET (MT_COLOR_TILDE);
1595 while (lines < bodylen) {
1597 if (option (OPTTILDE))
1601 move (lines + bodyoffset, SW);
1603 /* We are going to update the pager status bar, so it isn't
1604 * necessary to reset to normal color now. */
1606 redraw |= REDRAW_STATUS; /* need to update the % seen */
1609 if (redraw & REDRAW_STATUS) {
1610 /* print out the pager status bar */
1611 SETCOLOR (MT_COLOR_STATUS);
1612 BKGDSET (MT_COLOR_STATUS);
1613 CLEARLINE_WIN (statusoffset);
1614 if (IsHeader (extra)) {
1615 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1616 size_t l2 = sizeof (buffer);
1618 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1619 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1621 else if (IsMsgAttach (extra)) {
1622 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1623 size_t l2 = sizeof (buffer);
1625 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1626 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1628 move(statusoffset,SW);
1629 mutt_paddstr (COLS - 10 - SW, IsHeader (extra) ||
1630 IsMsgAttach (extra) ? buffer : banner);
1633 if (last_pos < sb.st_size - 1)
1634 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1636 addstr (topline == 0 ? "all)" : "end)");
1637 BKGDSET (MT_COLOR_NORMAL);
1638 SETCOLOR (MT_COLOR_NORMAL);
1641 if (redraw & REDRAW_SIDEBAR)
1644 if ((redraw & REDRAW_INDEX) && pager_index) {
1645 /* redraw the pager_index indicator, because the
1646 * flags for this message might have changed. */
1647 menu_redraw_current (pager_index);
1649 /* print out the pager_index status bar */
1650 menu_status_line (buffer, sizeof (buffer), pager_index, NONULL (Status));
1651 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), SW);
1652 SETCOLOR (MT_COLOR_STATUS);
1653 BKGDSET (MT_COLOR_STATUS);
1654 mutt_paddstr (COLS-SW, buffer);
1655 SETCOLOR (MT_COLOR_NORMAL);
1656 BKGDSET (MT_COLOR_NORMAL);
1658 /* if we're not using the index, update every time */
1664 if (option(OPTBRAILLEFRIENDLY)) {
1665 if (brailleLine!=-1) {
1666 move(brailleLine+1, 0);
1670 move (statusoffset, COLS-1);
1673 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1674 && lineInfo[curline].offset < sb.st_size-1) {
1675 if (TopLine - topline > lines)
1684 ch = km_dokey (MENU_PAGER);
1686 mutt_clear_error ();
1693 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1694 else if (SigWinch) {
1695 mutt_resize_screen ();
1697 /* Store current position. */
1699 for (i = 0; i <= topline; i++)
1700 if (!lineInfo[i].continuation)
1703 if (flags & M_PAGER_RETWINCH) {
1704 Resize = p_new(struct resize, 1);
1706 Resize->line = lines;
1707 Resize->SearchCompiled = SearchCompiled;
1708 Resize->SearchBack = SearchBack;
1711 rc = OP_REFORMAT_WINCH;
1714 for (i = 0; i < maxLine; i++) {
1715 lineInfo[i].offset = 0;
1716 lineInfo[i].type = -1;
1717 lineInfo[i].continuation = 0;
1718 lineInfo[i].chunks = 0;
1719 lineInfo[i].search_cnt = -1;
1720 lineInfo[i].quote = NULL;
1722 p_realloc(&lineInfo[i].syntax, 1);
1723 if (SearchCompiled && lineInfo[i].search)
1724 p_delete(&(lineInfo[i].search));
1730 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1735 clearok (stdscr, TRUE); /*force complete redraw */
1739 else if (ch == -1) {
1753 if (lineInfo[curline].offset < sb.st_size - 1) {
1754 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1756 else if (option (OPTPAGERSTOP)) {
1757 /* emulate "less -q" and don't go on to the next message. */
1758 mutt_error _("Bottom of message is shown.");
1761 /* end of the current message, so display the next message. */
1762 rc = OP_MAIN_NEXT_UNDELETED;
1770 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1773 mutt_error _("Top of message is shown.");
1777 if (lineInfo[curline].offset < sb.st_size - 1) {
1780 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1786 mutt_error _("Bottom of message is shown.");
1791 topline = upNLines (1, lineInfo, topline, hideQuoted);
1793 mutt_error _("Top of message is shown.");
1800 mutt_error _("Top of message is shown.");
1805 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1807 mutt_error _("Top of message is shown.");
1811 if (lineInfo[curline].offset < sb.st_size - 1) {
1812 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1814 else if (option (OPTPAGERSTOP)) {
1815 /* emulate "less -q" and don't go on to the next message. */
1816 mutt_error _("Bottom of message is shown.");
1819 /* end of the current message, so display the next message. */
1820 rc = OP_MAIN_NEXT_UNDELETED;
1825 case OP_SEARCH_NEXT:
1826 case OP_SEARCH_OPPOSITE:
1827 if (SearchCompiled) {
1829 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1830 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1831 /* searching forward */
1832 for (i = topline + 1; i < lastLine; i++) {
1833 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1834 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1841 mutt_error _("Not found.");
1844 /* searching backward */
1845 for (i = topline - 1; i >= 0; i--) {
1846 if ((!hideQuoted || (has_types &&
1847 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1848 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1855 mutt_error _("Not found.");
1858 if (lineInfo[topline].search_cnt > 0)
1859 SearchFlag = M_SEARCH;
1863 /* no previous search pattern, so fall through to search */
1866 case OP_SEARCH_REVERSE:
1867 m_strcpy(buffer, sizeof(buffer), searchbuf);
1868 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1869 _("Search: ")), buffer, sizeof (buffer),
1873 if (!strcmp (buffer, searchbuf)) {
1874 if (SearchCompiled) {
1875 /* do an implicit search-next */
1876 if (ch == OP_SEARCH)
1877 ch = OP_SEARCH_NEXT;
1879 ch = OP_SEARCH_OPPOSITE;
1888 m_strcpy(searchbuf, sizeof(searchbuf), buffer);
1890 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1891 if (ch == OP_SEARCH)
1893 else if (ch == OP_SEARCH_REVERSE)
1896 if (SearchCompiled) {
1897 regfree (&SearchRE);
1898 for (i = 0; i < lastLine; i++) {
1899 if (lineInfo[i].search)
1900 p_delete(&(lineInfo[i].search));
1901 lineInfo[i].search_cnt = -1;
1906 REGCOMP (&SearchRE, searchbuf,
1907 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1908 regerror (err, &SearchRE, buffer, sizeof (buffer));
1909 mutt_error ("%s", buffer);
1910 regfree (&SearchRE);
1911 for (i = 0; i < maxLine; i++) {
1913 if (lineInfo[i].search)
1914 p_delete(&(lineInfo[i].search));
1915 lineInfo[i].search_cnt = -1;
1922 /* update the search pointers */
1924 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1925 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1926 &QuoteList, &q_level,
1927 &force_redraw, &SearchRE) == 0) {
1929 redraw |= REDRAW_SIDEBAR;
1933 /* searching forward */
1934 for (i = topline; i < lastLine; i++) {
1935 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1936 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1944 /* searching backward */
1945 for (i = topline; i >= 0; i--) {
1946 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1947 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1955 if (lineInfo[topline].search_cnt == 0) {
1957 mutt_error _("Not found.");
1960 SearchFlag = M_SEARCH;
1962 redraw = REDRAW_BODY;
1965 case OP_SEARCH_TOGGLE:
1966 if (SearchCompiled) {
1967 SearchFlag ^= M_SEARCH;
1968 redraw = REDRAW_BODY;
1973 /* don't let the user enter the help-menu from the help screen! */
1976 mutt_help (MENU_PAGER);
1977 redraw = REDRAW_FULL;
1981 mutt_error _("Help is currently being shown.");
1984 case OP_PAGER_HIDE_QUOTED:
1986 hideQuoted ^= M_HIDE;
1987 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1988 topline = upNLines (1, lineInfo, topline, hideQuoted);
1990 redraw = REDRAW_BODY;
1994 case OP_PAGER_SKIP_QUOTED:
1997 int new_topline = topline;
1999 while ((new_topline < lastLine ||
2000 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2001 new_topline, &lastLine,
2002 &maxLine, M_TYPES, &QuoteList,
2003 &q_level, &force_redraw,
2005 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
2006 redraw |= REDRAW_SIDEBAR;
2011 mutt_error _("No more quoted text.");
2016 while ((new_topline < lastLine ||
2017 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2018 new_topline, &lastLine,
2019 &maxLine, M_TYPES, &QuoteList,
2020 &q_level, &force_redraw,
2022 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2024 redraw |= REDRAW_SIDEBAR;
2028 mutt_error _("No more unquoted text after quoted text.");
2032 topline = new_topline;
2036 case OP_PAGER_BOTTOM: /* move to the end of the file */
2037 if (lineInfo[curline].offset < sb.st_size - 1) {
2039 /* make sure the types are defined to the end of file */
2040 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2041 &maxLine, has_types,
2042 &QuoteList, &q_level, &force_redraw,
2045 redraw |= REDRAW_SIDEBAR;
2047 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2050 mutt_error _("Bottom of message is shown.");
2054 clearok (stdscr, TRUE);
2055 redraw = REDRAW_FULL;
2059 km_error_key (MENU_PAGER);
2062 /* --------------------------------------------------------------------
2063 * The following are operations on the current message rather than
2064 * adjusting the view of the message.
2067 case OP_BOUNCE_MESSAGE:
2068 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2070 if (IsMsgAttach (extra))
2071 mutt_attach_bounce (extra->fp, extra->hdr,
2072 extra->idx, extra->idxlen, extra->bdy);
2074 ci_bounce_message (extra->hdr, &redraw);
2078 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2080 if (IsMsgAttach (extra))
2081 mutt_attach_resend (extra->fp, extra->hdr,
2082 extra->idx, extra->idxlen, extra->bdy);
2084 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2085 redraw = REDRAW_FULL;
2088 case OP_CHECK_TRADITIONAL:
2089 CHECK_MODE (IsHeader (extra));
2090 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2092 rc = OP_CHECK_TRADITIONAL;
2096 case OP_CREATE_ALIAS:
2097 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2098 if (IsMsgAttach (extra))
2099 mutt_create_alias (extra->bdy->hdr->env, NULL);
2101 mutt_create_alias (extra->hdr->env, NULL);
2102 MAYBE_REDRAW (redraw);
2105 case OP_PURGE_MESSAGE:
2107 CHECK_MODE (IsHeader (extra));
2110 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2112 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2113 mutt_set_flag (Context, extra->hdr, M_PURGED,
2114 ch != OP_PURGE_MESSAGE ? 0 : 1);
2115 if (option (OPTDELETEUNTAG))
2116 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2117 redraw = REDRAW_STATUS | REDRAW_INDEX;
2118 if (option (OPTRESOLVE)) {
2120 rc = OP_MAIN_NEXT_UNDELETED;
2124 case OP_DELETE_THREAD:
2125 case OP_DELETE_SUBTHREAD:
2126 CHECK_MODE (IsHeader (extra));
2129 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2131 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2132 ch == OP_DELETE_THREAD ? 0 : 1);
2135 if (option (OPTDELETEUNTAG))
2136 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2137 ch == OP_DELETE_THREAD ? 0 : 1);
2138 if (option (OPTRESOLVE)) {
2139 rc = OP_MAIN_NEXT_UNDELETED;
2143 if (!option (OPTRESOLVE) && PagerIndexLines)
2144 redraw = REDRAW_FULL;
2146 redraw = REDRAW_STATUS | REDRAW_INDEX;
2150 case OP_DISPLAY_ADDRESS:
2151 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2152 if (IsMsgAttach (extra))
2153 mutt_display_address (extra->bdy->hdr->env);
2155 mutt_display_address (extra->hdr->env);
2158 case OP_ENTER_COMMAND:
2159 old_smart_wrap = option (OPTWRAP);
2160 old_markers = option (OPTMARKERS);
2161 old_PagerIndexLines = PagerIndexLines;
2163 CurrentMenu = MENU_PAGER;
2164 mutt_enter_command ();
2166 if (option (OPTNEEDRESORT)) {
2167 unset_option (OPTNEEDRESORT);
2168 CHECK_MODE (IsHeader (extra));
2169 set_option (OPTNEEDRESORT);
2172 if (old_PagerIndexLines != PagerIndexLines) {
2174 mutt_menuDestroy (&pager_index);
2178 if (option (OPTWRAP) != old_smart_wrap ||
2179 option (OPTMARKERS) != old_markers) {
2180 if (flags & M_PAGER_RETWINCH) {
2182 rc = OP_REFORMAT_WINCH;
2186 /* count the real lines above */
2188 for (i = 0; i <= topline; i++) {
2189 if (!lineInfo[i].continuation)
2193 /* we need to restart the whole thing */
2194 for (i = 0; i < maxLine; i++) {
2195 lineInfo[i].offset = 0;
2196 lineInfo[i].type = -1;
2197 lineInfo[i].continuation = 0;
2198 lineInfo[i].chunks = 0;
2199 lineInfo[i].search_cnt = -1;
2200 lineInfo[i].quote = NULL;
2202 p_realloc(&(lineInfo[i].syntax), 1);
2203 if (SearchCompiled && lineInfo[i].search)
2204 p_delete(&(lineInfo[i].search));
2207 if (SearchCompiled) {
2208 regfree (&SearchRE);
2213 /* try to keep the old position */
2216 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2217 &lastLine, &maxLine,
2218 (has_types ? M_TYPES : 0),
2219 &QuoteList, &q_level, &force_redraw,
2221 redraw |= REDRAW_SIDEBAR;
2222 if (!lineInfo[topline].continuation)
2231 if (option (OPTFORCEREDRAWPAGER))
2232 redraw = REDRAW_FULL;
2233 unset_option (OPTFORCEREDRAWINDEX);
2234 unset_option (OPTFORCEREDRAWPAGER);
2237 case OP_FLAG_MESSAGE:
2238 CHECK_MODE (IsHeader (extra));
2241 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2243 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2244 redraw = REDRAW_STATUS | REDRAW_INDEX;
2245 if (option (OPTRESOLVE)) {
2247 rc = OP_MAIN_NEXT_UNDELETED;
2252 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2253 if (IsAttach (extra))
2254 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2256 mutt_pipe_message (extra->hdr);
2257 MAYBE_REDRAW (redraw);
2261 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2262 if (IsAttach (extra))
2263 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2265 mutt_print_message (extra->hdr);
2269 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2271 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2272 redraw = REDRAW_FULL;
2277 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2279 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2280 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2281 query_quadoption (OPT_TOMODERATED,
2283 ("Posting to this group not allowed, may be moderated. Continue?"))
2286 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2287 redraw = REDRAW_FULL;
2290 case OP_FORWARD_TO_GROUP:
2291 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2293 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2294 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2295 query_quadoption (OPT_TOMODERATED,
2297 ("Posting to this group not allowed, may be moderated. Continue?"))
2300 if (IsMsgAttach (extra))
2301 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2302 extra->idxlen, extra->bdy, SENDNEWS);
2304 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2306 redraw = REDRAW_FULL;
2310 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2313 if (IsMsgAttach (extra))
2314 followup_to = extra->bdy->hdr->env->followup_to;
2316 followup_to = extra->hdr->env->followup_to;
2318 if (!followup_to || m_strcasecmp(followup_to, "poster") ||
2319 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2320 _("Reply by mail as poster prefers?")) != M_YES) {
2321 if (extra->ctx && extra->ctx->magic == M_NNTP
2322 && !((NNTP_DATA *) extra->ctx->data)->allowed
2323 && query_quadoption (OPT_TOMODERATED,
2325 ("Posting to this group not allowed, may be moderated. Continue?"))
2328 if (IsMsgAttach (extra))
2329 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2330 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2332 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2333 extra->ctx, extra->hdr);
2334 redraw = REDRAW_FULL;
2340 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2342 if (IsMsgAttach (extra))
2343 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2344 extra->idxlen, extra->bdy, SENDREPLY);
2346 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2347 redraw = REDRAW_FULL;
2350 case OP_RECALL_MESSAGE:
2351 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2353 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2354 redraw = REDRAW_FULL;
2357 case OP_GROUP_REPLY:
2358 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2360 if (IsMsgAttach (extra))
2361 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2362 extra->idxlen, extra->bdy,
2363 SENDREPLY | SENDGROUPREPLY);
2365 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2367 redraw = REDRAW_FULL;
2371 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2373 if (IsMsgAttach (extra))
2374 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2375 extra->idxlen, extra->bdy,
2376 SENDREPLY | SENDLISTREPLY);
2378 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2380 redraw = REDRAW_FULL;
2383 case OP_FORWARD_MESSAGE:
2384 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2386 if (IsMsgAttach (extra))
2387 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2388 extra->idxlen, extra->bdy, 0);
2390 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2391 redraw = REDRAW_FULL;
2394 case OP_DECRYPT_SAVE:
2396 if (IsAttach (extra)) {
2397 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2402 case OP_COPY_MESSAGE:
2403 case OP_DECODE_SAVE:
2404 case OP_DECODE_COPY:
2405 case OP_DECRYPT_COPY:
2406 CHECK_MODE (IsHeader (extra));
2407 if (mutt_save_message (extra->hdr,
2408 (ch == OP_DECRYPT_SAVE) ||
2409 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2410 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2411 (ch == OP_DECRYPT_SAVE)
2412 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2413 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2414 || ch == OP_DECRYPT_SAVE)) {
2415 if (option (OPTRESOLVE)) {
2417 rc = OP_MAIN_NEXT_UNDELETED;
2420 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2422 MAYBE_REDRAW (redraw);
2425 case OP_SHELL_ESCAPE:
2426 mutt_shell_escape ();
2427 MAYBE_REDRAW (redraw);
2431 CHECK_MODE (IsHeader (extra));
2432 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2434 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2435 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2436 ? NULL : Context->last_tag);
2438 redraw = REDRAW_STATUS | REDRAW_INDEX;
2439 if (option (OPTRESOLVE)) {
2446 CHECK_MODE (IsHeader (extra));
2449 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2451 if (extra->hdr->read || extra->hdr->old)
2452 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2454 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2456 Context->msgnotreadyet = -1;
2457 redraw = REDRAW_STATUS | REDRAW_INDEX;
2458 if (option (OPTRESOLVE)) {
2460 rc = OP_MAIN_NEXT_UNDELETED;
2465 CHECK_MODE (IsHeader (extra));
2468 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2470 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2471 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2472 redraw = REDRAW_STATUS | REDRAW_INDEX;
2473 if (option (OPTRESOLVE)) {
2479 case OP_UNDELETE_THREAD:
2480 case OP_UNDELETE_SUBTHREAD:
2481 CHECK_MODE (IsHeader (extra));
2484 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2486 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2487 ch == OP_UNDELETE_THREAD ? 0 : 1)
2488 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2489 ch == OP_UNDELETE_THREAD ? 0 : 1);
2492 if (option (OPTRESOLVE)) {
2493 rc = (ch == OP_DELETE_THREAD) ?
2494 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2498 if (!option (OPTRESOLVE) && PagerIndexLines)
2499 redraw = REDRAW_FULL;
2501 redraw = REDRAW_STATUS | REDRAW_INDEX;
2510 if (option (OPTFORCEBUFFYCHECK))
2513 redraw |= REDRAW_SIDEBAR;
2516 case OP_VIEW_ATTACHMENTS:
2517 if (flags & M_PAGER_ATTACHMENT) {
2519 rc = OP_ATTACH_COLLAPSE;
2522 CHECK_MODE (IsHeader (extra));
2523 mutt_view_attachments (extra->hdr);
2524 if (extra->hdr->attach_del)
2525 Context->changed = 1;
2526 redraw = REDRAW_FULL;
2531 CHECK_MODE (IsHeader (extra));
2533 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2534 redraw = REDRAW_FULL;
2538 case OP_FORGET_PASSPHRASE:
2539 crypt_forget_passphrase ();
2542 case OP_EXTRACT_KEYS:
2543 CHECK_MODE (IsHeader (extra));
2544 crypt_extract_keys_from_messages (extra->hdr);
2545 redraw = REDRAW_FULL;
2548 case OP_SIDEBAR_SCROLL_UP:
2549 case OP_SIDEBAR_SCROLL_DOWN:
2550 case OP_SIDEBAR_NEXT:
2551 case OP_SIDEBAR_NEXT_NEW:
2552 case OP_SIDEBAR_PREV:
2553 case OP_SIDEBAR_PREV_NEW:
2554 sidebar_scroll (ch);
2563 if (IsHeader (extra)) {
2564 Context->msgnotreadyet = -1;
2569 OldHdr = extra->hdr;
2573 cleanup_quote (&QuoteList);
2575 for (i = 0; i < maxLine; i++) {
2576 p_delete(&(lineInfo[i].syntax));
2577 if (SearchCompiled && lineInfo[i].search)
2578 p_delete(&(lineInfo[i].search));
2580 if (SearchCompiled) {
2581 regfree (&SearchRE);
2584 p_delete(&lineInfo);
2586 mutt_menuDestroy (&pager_index);
2587 return (rc != -1 ? rc : 0);