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.
18 #include <lib-lib/mem.h>
19 #include <lib-lib/str.h>
20 #include <lib-lib/macros.h>
21 #include <lib-lib/mapping.h>
25 #include "mutt_curses.h"
27 #include "mutt_menu.h"
31 #include "recvattach.h"
39 #include "imap_private.h"
42 #include "mutt_crypt.h"
45 #include "lib/debug.h"
55 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
57 #define IsAttach(x) (x && (x)->bdy)
58 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
59 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
60 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
61 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
62 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
64 static const char *Not_available_in_this_menu =
65 N_("Not available in this menu.");
66 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
67 static const char *Function_not_permitted_in_attach_message_mode =
68 N_("Function not permitted in attach-message mode.");
70 /* hack to return to position when returning from index to same message */
71 static int TopLine = 0;
72 static HEADER *OldHdr = NULL;
74 #define CHECK_MODE(x) if (!(x)) \
77 mutt_error _(Not_available_in_this_menu); \
81 #define CHECK_READONLY if (Context->readonly) \
84 mutt_error _(Mailbox_is_read_only); \
88 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
91 mutt_error _(Function_not_permitted_in_attach_message_mode); \
100 struct q_class_t *next, *prev;
101 struct q_class_t *down, *up;
116 struct syntax_t *syntax;
117 struct syntax_t *search;
118 struct q_class_t *quote;
121 #define ANSI_OFF (1<<0)
122 #define ANSI_BLINK (1<<1)
123 #define ANSI_BOLD (1<<2)
124 #define ANSI_UNDERLINE (1<<3)
125 #define ANSI_REVERSE (1<<4)
126 #define ANSI_COLOR (1<<5)
128 typedef struct _ansi_attr {
135 static short InHelp = 0;
137 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
138 static struct resize {
145 #define NumSigLines 4
147 static int check_sig (const char *s, struct line_t *info, int n)
151 while (n > 0 && count <= NumSigLines) {
152 if (info[n].type != MT_COLOR_SIGNATURE)
161 if (count > NumSigLines) {
162 /* check for a blank line */
176 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
177 int special, ansi_attr * a)
179 int def_color; /* color without syntax hilight */
180 int color; /* final color */
181 static int last_color; /* last color set */
182 int search = 0, i, m;
185 last_color = -1; /* force attrset() */
187 if (lineInfo[n].continuation) {
188 if (!cnt && option (OPTMARKERS)) {
189 SETCOLOR (MT_COLOR_MARKERS);
191 last_color = ColorDefs[MT_COLOR_MARKERS];
193 m = (lineInfo[n].syntax)[0].first;
194 cnt += (lineInfo[n].syntax)[0].last;
198 if (!(flags & M_SHOWCOLOR))
199 def_color = ColorDefs[MT_COLOR_NORMAL];
200 else if (lineInfo[m].type == MT_COLOR_HEADER)
201 def_color = (lineInfo[m].syntax)[0].color;
203 def_color = ColorDefs[lineInfo[m].type];
205 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
206 struct q_class_t *class = lineInfo[m].quote;
209 def_color = class->color;
211 while (class && class->length > cnt) {
212 def_color = class->color;
219 if (flags & M_SHOWCOLOR) {
220 for (i = 0; i < lineInfo[m].chunks; i++) {
221 /* we assume the chunks are sorted */
222 if (cnt > (lineInfo[m].syntax)[i].last)
224 if (cnt < (lineInfo[m].syntax)[i].first)
226 if (cnt != (lineInfo[m].syntax)[i].last) {
227 color = (lineInfo[m].syntax)[i].color;
230 /* don't break here, as cnt might be
231 * in the next chunk as well */
235 if (flags & M_SEARCH) {
236 for (i = 0; i < lineInfo[m].search_cnt; i++) {
237 if (cnt > (lineInfo[m].search)[i].last)
239 if (cnt < (lineInfo[m].search)[i].first)
241 if (cnt != (lineInfo[m].search)[i].last) {
242 color = ColorDefs[MT_COLOR_SEARCH];
249 /* handle "special" bold & underlined characters */
250 if (special || a->attr) {
252 if ((a->attr & ANSI_COLOR)) {
254 a->pair = mutt_alloc_color (a->fg, a->bg);
256 if (a->attr & ANSI_BOLD)
261 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
262 if (ColorDefs[MT_COLOR_BOLD] && !search)
263 color = ColorDefs[MT_COLOR_BOLD];
267 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
268 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
269 color = ColorDefs[MT_COLOR_UNDERLINE];
271 color ^= A_UNDERLINE;
273 else if (a->attr & ANSI_REVERSE) {
276 else if (a->attr & ANSI_BLINK) {
279 else if (a->attr & ANSI_OFF) {
284 if (color != last_color) {
290 static void append_line (struct line_t *lineInfo, int n, int cnt)
294 lineInfo[n + 1].type = lineInfo[n].type;
295 (lineInfo[n + 1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
296 lineInfo[n + 1].continuation = 1;
298 /* find the real start of the line */
299 for (m = n; m >= 0; m--)
300 if (lineInfo[m].continuation == 0)
303 (lineInfo[n + 1].syntax)[0].first = m;
304 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
305 cnt + (lineInfo[n].syntax)[0].last : cnt;
308 static void new_class_color (struct q_class_t *class, int *q_level)
310 class->index = (*q_level)++;
311 class->color = ColorQuote[class->index % ColorQuoteUsed];
315 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
316 int index, int *q_level)
318 struct q_class_t *q_list;
321 new_class->index = -1;
324 if (q_list->index >= index) {
326 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
329 q_list = q_list->down;
330 else if (q_list->next)
331 q_list = q_list->next;
333 while (!q_list->next) {
339 q_list = q_list->next;
343 new_class->index = index;
344 new_class->color = ColorQuote[index % ColorQuoteUsed];
348 static void cleanup_quote (struct q_class_t **QuoteList)
350 struct q_class_t *ptr;
353 if ((*QuoteList)->down)
354 cleanup_quote (&((*QuoteList)->down));
355 ptr = (*QuoteList)->next;
356 if ((*QuoteList)->prefix)
357 p_delete(&(*QuoteList)->prefix);
365 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
366 const char *qptr, int length,
367 int *force_redraw, int *q_level)
369 struct q_class_t *q_list = *QuoteList;
370 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
372 int offset, tail_lng;
375 if (ColorQuoteUsed <= 1) {
376 /* not much point in classifying quotes... */
378 if (*QuoteList == NULL) {
379 class = p_new(struct q_class_t, 1);
380 class->color = ColorQuote[0];
386 /* Did I mention how much I like emulating Lisp in C? */
388 /* classify quoting prefix */
390 if (length <= q_list->length) {
391 /* case 1: check the top level nodes */
393 if (m_strncmp(qptr, q_list->prefix, length) == 0) {
394 if (length == q_list->length)
395 return q_list; /* same prefix: return the current class */
397 /* found shorter prefix */
399 /* add a node above q_list */
400 tmp = p_new(struct q_class_t, 1);
401 tmp->prefix = p_dupstr(qptr, length);
402 tmp->length = length;
404 /* replace q_list by tmp in the top level list */
406 tmp->next = q_list->next;
407 q_list->next->prev = tmp;
410 tmp->prev = q_list->prev;
411 q_list->prev->next = tmp;
414 /* make q_list a child of tmp */
418 /* q_list has no siblings for now */
422 /* update the root if necessary */
423 if (q_list == *QuoteList)
426 index = q_list->index;
428 /* tmp should be the return class too */
431 /* next class to test; if tmp is a shorter prefix for another
432 * node, that node can only be in the top level list, so don't
433 * go down after this point
438 /* found another branch for which tmp is a shorter prefix */
440 /* save the next sibling for later */
443 /* unlink q_list from the top level list */
445 q_list->next->prev = q_list->prev;
447 q_list->prev->next = q_list->next;
449 /* at this point, we have a tmp->down; link q_list to it */
451 /* sibling order is important here, q_list should be linked last */
459 index = q_list->index;
461 /* next class to test; as above, we shouldn't go down */
465 /* we found a shorter prefix, so certain quotes have changed classes */
470 /* shorter, but not a substring of the current class: try next */
471 q_list = q_list->next;
476 /* case 2: try subclassing the current top level node */
478 /* tmp != NULL means we already found a shorter prefix at case 1 */
480 && m_strncmp(qptr, q_list->prefix, q_list->length) == 0) {
481 /* ok, it's a subclass somewhere on this branch */
484 offset = q_list->length;
486 q_list = q_list->down;
487 tail_lng = length - offset;
488 tail_qptr = (char *) qptr + offset;
491 if (length <= q_list->length) {
492 if (m_strncmp(tail_qptr, (q_list->prefix) + offset, tail_lng)
494 /* same prefix: return the current class */
495 if (length == q_list->length)
498 /* found shorter common prefix */
500 /* add a node above q_list */
501 tmp = p_new(struct q_class_t, 1);
502 tmp->prefix = p_dupstr(qptr, length);
503 tmp->length = length;
505 /* replace q_list by tmp */
507 tmp->next = q_list->next;
508 q_list->next->prev = tmp;
511 tmp->prev = q_list->prev;
512 q_list->prev->next = tmp;
515 /* make q_list a child of tmp */
517 tmp->up = q_list->up;
519 if (tmp->up->down == q_list)
522 /* q_list has no siblings */
526 index = q_list->index;
528 /* tmp should be the return class too */
531 /* next class to test */
535 /* found another branch for which tmp is a shorter prefix */
537 /* save the next sibling for later */
540 /* unlink q_list from the top level list */
542 q_list->next->prev = q_list->prev;
544 q_list->prev->next = q_list->next;
546 /* at this point, we have a tmp->down; link q_list to it */
555 index = q_list->index;
557 /* next class to test */
561 /* we found a shorter prefix, so we need a redraw */
566 q_list = q_list->next;
571 /* longer than the current prefix: try subclassing it */
573 && m_strncmp(tail_qptr, (q_list->prefix) + offset,
574 q_list->length - offset) == 0) {
575 /* still a subclass: go down one level */
577 offset = q_list->length;
579 q_list = q_list->down;
580 tail_lng = length - offset;
581 tail_qptr = (char *) qptr + offset;
586 /* nope, try the next prefix */
587 q_list = q_list->next;
593 /* still not found so far: add it as a sibling to the current node */
595 tmp = p_new(struct q_class_t, 1);
596 tmp->prefix = p_dupstr(qptr, length);
597 tmp->length = length;
600 tmp->next = ptr->down;
601 ptr->down->prev = tmp;
606 new_class_color (tmp, q_level);
612 shift_class_colors (*QuoteList, tmp, index, q_level);
618 /* nope, try the next prefix */
619 q_list = q_list->next;
626 /* not found so far: add it as a top level class */
627 class = p_new(struct q_class_t, 1);
628 class->prefix = p_dupstr(qptr, length);
629 class->length = length;
630 new_class_color (class, q_level);
633 class->next = *QuoteList;
634 (*QuoteList)->prev = class;
640 shift_class_colors (*QuoteList, tmp, index, q_level);
645 static int brailleLine = -1;
646 static int brailleCol = -1;
648 static int check_attachment_marker (char *);
651 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
652 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
655 COLOR_LINE *color_line;
656 regmatch_t pmatch[1], smatch[1];
657 int found, offset, null_rx, i;
659 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
660 if (buf[0] == '\n') {
661 lineInfo[n].type = MT_COLOR_NORMAL;
662 getyx(stdscr, brailleLine, brailleCol);
664 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
665 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
666 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
669 lineInfo[n].type = MT_COLOR_HDEFAULT;
670 color_line = ColorHdrList;
672 if (REGEXEC (&color_line->rx, buf) == 0) {
673 lineInfo[n].type = MT_COLOR_HEADER;
674 lineInfo[n].syntax[0].color = color_line->pair;
677 color_line = color_line->next;
681 else if (m_strncmp("\033[0m", raw, 4) == 0) /* a little hack... */
682 lineInfo[n].type = MT_COLOR_NORMAL;
684 else if (m_strncmp("[-- ", buf, 4) == 0)
685 lineInfo[n].type = MT_COLOR_ATTACHMENT;
687 else if (check_attachment_marker ((char *) raw) == 0)
688 lineInfo[n].type = MT_COLOR_ATTACHMENT;
690 else if (m_strcmp("-- \n", buf) == 0
691 || m_strcmp("-- \r\n", buf) == 0) {
694 lineInfo[n].type = MT_COLOR_SIGNATURE;
695 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
696 (lineInfo[i].type == MT_COLOR_NORMAL ||
697 lineInfo[i].type == MT_COLOR_QUOTED ||
698 lineInfo[i].type == MT_COLOR_HEADER)) {
700 if (lineInfo[i].chunks) {
701 lineInfo[i].chunks = 0;
702 p_realloc(&(lineInfo[n].syntax), 1);
704 lineInfo[i++].type = MT_COLOR_SIGNATURE;
707 else if (check_sig (buf, lineInfo, n - 1) == 0)
708 lineInfo[n].type = MT_COLOR_SIGNATURE;
709 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
710 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
711 if (smatch[0].rm_so > 0) {
714 /* hack to avoid making an extra copy of buf */
715 c = buf[smatch[0].rm_so];
716 buf[smatch[0].rm_so] = 0;
718 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
719 if (q_classify && lineInfo[n].quote == NULL)
720 lineInfo[n].quote = classify_quote (QuoteList,
721 buf + pmatch[0].rm_so,
723 pmatch[0].rm_so, force_redraw,
725 lineInfo[n].type = MT_COLOR_QUOTED;
728 lineInfo[n].type = MT_COLOR_NORMAL;
730 buf[smatch[0].rm_so] = c;
733 lineInfo[n].type = MT_COLOR_NORMAL;
736 if (q_classify && lineInfo[n].quote == NULL)
737 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
738 pmatch[0].rm_eo - pmatch[0].rm_so,
739 force_redraw, q_level);
740 lineInfo[n].type = MT_COLOR_QUOTED;
744 lineInfo[n].type = MT_COLOR_NORMAL;
747 if (lineInfo[n].type == MT_COLOR_NORMAL ||
748 lineInfo[n].type == MT_COLOR_QUOTED) {
752 lineInfo[n].chunks = 0;
759 color_line = ColorBodyList;
761 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
762 (offset ? REG_NOTBOL : 0)) == 0) {
763 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
765 if (++(lineInfo[n].chunks) > 1)
766 p_realloc(&(lineInfo[n].syntax), lineInfo[n].chunks);
768 i = lineInfo[n].chunks - 1;
769 pmatch[0].rm_so += offset;
770 pmatch[0].rm_eo += offset;
772 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
773 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
774 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
775 (lineInfo[n].syntax)[i].color = color_line->pair;
776 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
777 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
783 null_rx = 1; /* empty regexp; don't add it, but keep looking */
785 color_line = color_line->next;
789 offset++; /* avoid degenerate cases */
791 offset = (lineInfo[n].syntax)[i].last;
792 } while (found || null_rx);
796 static int is_ansi (unsigned char *buf)
798 while (*buf && (isdigit (*buf) || *buf == ';'))
800 return (*buf == 'm');
803 static int check_attachment_marker (char *p)
805 char *q = AttachmentMarker;
807 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
808 return (int) (*p - *q);
811 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
815 while (isdigit (buf[x]) || buf[x] == ';')
818 /* Character Attributes */
819 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
823 mutt_free_color (a->fg, a->bg);
831 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
832 a->attr |= ANSI_BOLD;
835 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
836 a->attr |= ANSI_UNDERLINE;
839 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
840 a->attr |= ANSI_BLINK;
843 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
844 a->attr |= ANSI_REVERSE;
847 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
852 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
855 mutt_free_color (a->fg, a->bg);
858 a->attr |= ANSI_COLOR;
859 if (buf[pos + 1] != '9')
860 a->fg = buf[pos + 1] - '0';
863 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
866 mutt_free_color (a->fg, a->bg);
869 a->attr |= ANSI_COLOR;
870 if (buf[pos + 1] != '9')
871 a->bg = buf[pos + 1] - '0';
875 while (pos < x && buf[pos] != ';')
885 /* trim tail of buf so that it contains complete multibyte characters */
886 static int trim_incomplete_mbyte(unsigned char *buf, size_t len) {
890 p_clear(&mbstate, 1);
891 for (; len > 0; buf += k, len -= k) {
892 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
895 else if (k == -1 || k == 0)
904 fill_buffer (FILE * f, off_t *last_pos, off_t offset, unsigned char *buf,
905 unsigned char *fmt, size_t blen, int *buf_ready)
908 static int b_read = 0;
910 if (*buf_ready == 0) {
912 if (offset != *last_pos)
913 fseeko (f, offset, 0);
914 if (fgets ((char *) buf, blen - 1, f) == NULL) {
918 *last_pos = ftello (f);
919 b_read = (int) (*last_pos - offset);
922 /* incomplete mbyte characters trigger a segfault in regex processing for
923 * certain versions of glibc. Trim them if necessary. */
924 if (b_read == blen - 2)
925 b_read -= trim_incomplete_mbyte(buf, b_read);
927 /* copy "buf" to "fmt", but without bold and underline controls */
930 if (*p == '\010' && (p > buf)) {
931 if (*(p + 1) == '_') /* underline */
933 else if (*(p + 1)) { /* bold or overstrike */
934 *(fmt - 1) = *(p + 1);
940 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
941 while (*p++ != 'm') /* skip ANSI sequence */
944 else if (*p == '\033' && *(p + 1) == ']'
945 && check_attachment_marker ((char *) p) == 0) {
946 debug_print (2, ("seen attachment marker.\n"));
947 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
964 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
965 int flags, ansi_attr * pa, int cnt,
966 int *pspace, int *pvch, int *pcol, int *pspecial)
968 int space = -1; /* index of the last space or TAB */
969 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
970 int ch, vch, k, last_special = -1, special = 0, t;
974 int wrap_cols = COLS;
976 if (!(flags & (M_SHOWFLAT)))
977 wrap_cols -= WrapMargin;
983 /* FIXME: this should come from lineInfo */
984 p_clear(&mbstate, 1);
986 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
987 /* Handle ANSI sequences */
988 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
989 is_ansi (buf + ch + 2))
990 ch = grok_ansi (buf, ch + 2, pa) + 1;
992 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
993 check_attachment_marker ((char *) buf + ch) == 0) {
994 while (buf[ch++] != '\a')
999 /* is anything left to do? */
1003 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
1004 if (k == -2 || k == -1) {
1005 debug_print (1, ("mbrtowc returned %d; errno = %d.\n", k, errno));
1006 if (col + 4 > wrap_cols)
1010 printw ("\\%03o", buf[ch]);
1017 /* Handle backspace */
1019 if (IsWPrint (wc)) {
1024 while ((wc1 = 0, mbstate1 = mbstate,
1026 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
1027 &mbstate1), k1 - k > 0 && wc1 == '\b')
1029 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1030 &mbstate1), k2 > 0 && IsWPrint (wc1))) {
1032 special |= (wc == '_' && special & A_UNDERLINE)
1033 ? A_UNDERLINE : A_BOLD;
1035 else if (wc == '_' || wc1 == '_') {
1036 special |= A_UNDERLINE;
1037 wc = (wc1 == '_') ? wc : wc1;
1040 /* special = 0; / * overstrike: nothing to do! */
1050 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1051 special || last_special || pa->attr)) {
1052 resolve_color (*lineInfo, n, vch, flags, special, pa);
1053 last_special = special;
1056 if (IsWPrint (wc)) {
1060 if (col + t > wrap_cols)
1066 else if (wc == '\n')
1068 else if (wc == '\t') {
1074 for (; col < t; col++)
1079 else if (wc < 0x20 || wc == 0x7f) {
1080 if (col + 2 > wrap_cols)
1084 printw ("^%c", ('@' + wc) & 0x7f);
1086 else if (wc < 0x100) {
1087 if (col + 4 > wrap_cols)
1091 printw ("\\%03o", wc);
1094 if (col + 1 > wrap_cols)
1098 addch (replacement_char ());
1104 *pspecial = special;
1110 * flags M_SHOWFLAT, show characters (used for displaying help)
1111 * M_SHOWCOLOR, show characters in color
1112 * otherwise don't show characters
1113 * M_HIDE, don't show quoted text
1114 * M_SEARCH, resolve search patterns
1115 * M_TYPES, compute line's type
1116 * M_PAGER_NSKIP, keeps leading whitespace
1117 * M_PAGER_MARKER, eventually show markers
1120 * -1 EOF was reached
1121 * 0 normal exit, line was not displayed
1122 * >0 normal exit, line was displayed
1126 display_line (FILE * f, off_t *last_pos, struct line_t **lineInfo, int n,
1127 int *last, int *max, int flags, struct q_class_t **QuoteList,
1128 int *q_level, int *force_redraw, regex_t * SearchRE)
1130 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1131 unsigned char *buf_ptr = buf;
1132 int ch, vch, col, cnt, b_read;
1133 int buf_ready = 0, change_last = 0;
1138 ansi_attr a = { 0, 0, 0, -1 };
1139 regmatch_t pmatch[1];
1146 if (*last == *max) {
1147 p_realloc(lineInfo, *max += LINES);
1148 for (ch = *last; ch < *max; ch++) {
1149 p_clear(&(*lineInfo)[ch], 1);
1150 (*lineInfo)[ch].type = -1;
1151 (*lineInfo)[ch].search_cnt = -1;
1152 (*lineInfo)[ch].syntax = p_new(struct syntax_t, 1);
1153 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1158 /* only do color hiliting if we are viewing a message */
1159 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1160 if ((*lineInfo)[n].type == -1) {
1161 /* determine the line class */
1163 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1170 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1171 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1173 /* avoid race condition for continuation lines when scrolling up */
1175 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1177 (*lineInfo)[m].type = (*lineInfo)[n].type;
1180 /* this also prevents searching through the hidden lines */
1181 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1182 flags = 0; /* M_NOSHOW */
1185 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1186 * don't want to compute it every time M_TYPES is set, since this
1187 * would slow down the "bottom" function unacceptably. A compromise
1188 * solution is hence to call regexec() again, just to find out the
1189 * length of the quote prefix.
1191 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1192 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1195 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1201 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1202 (*lineInfo)[n].quote = classify_quote (QuoteList,
1203 (char *) fmt + pmatch[0].rm_so,
1204 pmatch[0].rm_eo - pmatch[0].rm_so,
1205 force_redraw, q_level);
1208 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1209 && (*lineInfo)[n].search_cnt == -1) {
1211 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1219 (*lineInfo)[n].search_cnt = 0;
1221 (SearchRE, (char *) fmt + offset, 1, pmatch,
1222 (offset ? REG_NOTBOL : 0)) == 0) {
1223 if (++((*lineInfo)[n].search_cnt) > 1)
1224 p_realloc(&(*lineInfo)[n].search, (*lineInfo)[n].search_cnt);
1226 (*lineInfo)[n].search = p_new(struct syntax_t, 1);
1227 pmatch[0].rm_so += offset;
1228 pmatch[0].rm_eo += offset;
1229 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1231 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1234 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1235 offset++; /* avoid degenerate cases */
1237 offset = pmatch[0].rm_eo;
1243 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1244 /* we've already scanned this line, so just exit */
1247 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1248 /* no need to try to display this line... */
1249 return (1); /* fake display */
1252 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1253 sizeof (buf), &buf_ready)) < 0) {
1259 /* now chose a good place to break the line */
1261 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1263 buf_ptr = buf + cnt;
1265 /* move the break point only if smart_wrap is set */
1266 if (option (OPTWRAP)) {
1268 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1269 && buf[cnt] != '\r') {
1271 /* skip trailing blanks */
1272 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1274 /* a very long word with leading spaces causes infinite wrapping */
1275 if ((!ch) && (flags & M_PAGER_NSKIP))
1276 buf_ptr = buf + cnt;
1281 buf_ptr = buf + cnt; /* a very long word... */
1283 if (!(flags & M_PAGER_NSKIP))
1284 /* skip leading blanks on the next line too */
1285 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1289 if (*buf_ptr == '\r')
1291 if (*buf_ptr == '\n')
1294 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1295 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1296 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1298 /* if we don't need to display the line we are done */
1299 if (!(flags & M_SHOW))
1302 /* display the line */
1303 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1305 /* avoid a bug in ncurses... */
1306 #ifndef USE_SLANG_CURSES
1308 SETCOLOR (MT_COLOR_NORMAL);
1313 /* end the last color pattern (needed by S-Lang) */
1314 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1315 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1318 * Fill the blank space at the end of the line with the prevailing color.
1319 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1320 * to make sure to reset the color *after* that
1322 if (flags & M_SHOWCOLOR) {
1323 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1324 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1325 def_color = ((*lineInfo)[m].syntax)[0].color;
1327 def_color = ColorDefs[(*lineInfo)[m].type];
1329 attrset (def_color);
1331 bkgdset (def_color | ' ');
1335 /* ncurses always wraps lines when you get to the right side of the
1336 * screen, but S-Lang seems to only wrap if the next character is *not*
1339 #ifndef USE_SLANG_CURSES
1345 * reset the color back to normal. This *must* come after the
1346 * addch('\n'), otherwise the color for this line will not be
1347 * filled to the right margin.
1349 if (flags & M_SHOWCOLOR) {
1350 SETCOLOR (MT_COLOR_NORMAL);
1351 BKGDSET (MT_COLOR_NORMAL);
1354 /* build a return code */
1355 if (!(flags & M_SHOW))
1361 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1363 while (cur > 0 && nlines > 0) {
1365 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1372 static struct mapping_t PagerHelp[] = {
1373 {N_("Exit"), OP_EXIT},
1374 {N_("PrevPg"), OP_PREV_PAGE},
1375 {N_("NextPg"), OP_NEXT_PAGE},
1378 static struct mapping_t PagerHelpExtra[] = {
1379 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1380 {N_("Del"), OP_DELETE},
1381 {N_("Reply"), OP_REPLY},
1382 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1387 static struct mapping_t PagerNewsHelpExtra[] = {
1388 {N_("Post"), OP_POST},
1389 {N_("Followup"), OP_FOLLOWUP},
1390 {N_("Del"), OP_DELETE},
1391 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1398 /* This pager is actually not so simple as it once was. It now operates in
1399 two modes: one for viewing messages and the other for viewing help. These
1400 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1401 is there so that we can do operations on the current message without the
1402 need to pop back out to the main-menu. */
1404 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1406 static char searchbuf[STRING];
1407 char buffer[LONG_STRING];
1408 char helpstr[SHORT_STRING * 2];
1409 char tmphelp[SHORT_STRING * 2];
1410 int maxLine, lastLine = 0;
1411 struct line_t *lineInfo;
1412 struct q_class_t *QuoteList = NULL;
1413 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1414 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1416 int redraw = REDRAW_FULL;
1418 off_t last_pos = 0, last_offset = 0;
1419 int old_smart_wrap, old_markers;
1422 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1423 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1425 int bodyoffset = 1; /* offset of first line of real text */
1426 int statusoffset = 0; /* offset for the status bar */
1427 int helpoffset = LINES - 2; /* offset for the help bar. */
1428 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1430 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1431 int indexoffset = 0; /* offset for the PI */
1432 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1433 int indicator = indexlen / 3; /* the indicator line of the PI */
1434 int old_PagerIndexLines; /* some people want to resize it
1435 * while inside the pager... */
1441 if (!(flags & M_SHOWCOLOR))
1442 flags |= M_SHOWFLAT;
1444 if ((fp = fopen (fname, "r")) == NULL) {
1445 mutt_perror (fname);
1449 if (stat (fname, &sb) != 0) {
1450 mutt_perror (fname);
1456 /* Initialize variables */
1458 if (IsHeader (extra) && !extra->hdr->read) {
1459 Context->msgnotreadyet = extra->hdr->msgno;
1460 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1463 lineInfo = p_new(struct line_t, maxLine = LINES);
1464 for (i = 0; i < maxLine; i++) {
1465 p_clear(&lineInfo[i], 1);
1466 lineInfo[i].type = -1;
1467 lineInfo[i].search_cnt = -1;
1468 lineInfo[i].syntax = p_new(struct syntax_t, 1);
1469 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1472 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1473 if (IsHeader (extra)) {
1474 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1475 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1478 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1481 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1484 m_strcpy(tmphelp, sizeof(tmphelp), helpstr);
1485 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1486 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1496 if (redraw & REDRAW_FULL) {
1497 SETCOLOR (MT_COLOR_NORMAL);
1498 /* clear() doesn't optimize screen redraws */
1502 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1503 indexlen = Context->vcount + 1;
1505 indexlen = PagerIndexLines;
1507 indicator = indexlen / 3;
1509 if (option (OPTSTATUSONTOP)) {
1511 statusoffset = IsHeader (extra) ? indexlen : 0;
1512 bodyoffset = statusoffset + 1;
1513 helpoffset = LINES - 2;
1514 bodylen = helpoffset - bodyoffset;
1515 if (!option (OPTHELP))
1521 statusoffset = LINES - 2;
1522 if (!option (OPTHELP))
1524 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1525 bodylen = statusoffset - bodyoffset;
1528 if (option (OPTHELP)) {
1529 SETCOLOR (MT_COLOR_STATUS);
1530 move (helpoffset, SW);
1531 mutt_paddstr (COLS-SW, helpstr);
1532 SETCOLOR (MT_COLOR_NORMAL);
1535 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1536 if (Resize != NULL) {
1537 if ((SearchCompiled = Resize->SearchCompiled)) {
1539 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1540 SearchFlag = M_SEARCH;
1541 SearchBack = Resize->SearchBack;
1543 lines = Resize->line;
1544 redraw |= REDRAW_SIGWINCH;
1550 if (IsHeader (extra) && PagerIndexLines) {
1551 if (index == NULL) {
1552 /* only allocate the space if/when we need the index.
1553 Initialise the menu as per the main index */
1554 index = mutt_new_menu ();
1555 index->menu = MENU_MAIN;
1556 index->make_entry = index_make_entry;
1557 index->color = index_color;
1558 index->max = Context->vcount;
1559 index->current = extra->hdr->virtual;
1562 SETCOLOR (MT_COLOR_NORMAL);
1563 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1565 index->pagelen = indexlen - 1;
1567 /* some fudge to work out where abouts the indicator should go */
1568 if (index->current - indicator < 0)
1570 else if (index->max - index->current < index->pagelen - indicator)
1571 index->top = index->max - index->pagelen;
1573 index->top = index->current - indicator;
1575 menu_redraw_index (index);
1578 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1582 if (redraw & REDRAW_SIGWINCH) {
1585 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1586 has_types | SearchFlag, &QuoteList, &q_level,
1587 &force_redraw, &SearchRE) == 0) {
1588 if (!lineInfo[i].continuation && ++j == lines) {
1593 redraw |= REDRAW_SIDEBAR;
1597 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1599 move (bodyoffset, SW);
1600 curline = oldtopline = topline;
1604 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1605 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1607 (flags & M_DISPLAYFLAGS) | hideQuoted |
1608 SearchFlag, &QuoteList, &q_level, &force_redraw,
1612 move (lines + bodyoffset, SW);
1613 redraw |= REDRAW_SIDEBAR;
1615 last_offset = lineInfo[curline].offset;
1616 } while (force_redraw);
1618 SETCOLOR (MT_COLOR_TILDE);
1619 BKGDSET (MT_COLOR_TILDE);
1620 while (lines < bodylen) {
1622 if (option (OPTTILDE))
1626 move (lines + bodyoffset, SW);
1628 /* We are going to update the pager status bar, so it isn't
1629 * necessary to reset to normal color now. */
1631 redraw |= REDRAW_STATUS; /* need to update the % seen */
1634 if (redraw & REDRAW_STATUS) {
1635 /* print out the pager status bar */
1636 SETCOLOR (MT_COLOR_STATUS);
1637 BKGDSET (MT_COLOR_STATUS);
1638 CLEARLINE_WIN (statusoffset);
1639 if (IsHeader (extra)) {
1640 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1641 size_t l2 = sizeof (buffer);
1643 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1644 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1646 else if (IsMsgAttach (extra)) {
1647 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1648 size_t l2 = sizeof (buffer);
1650 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1651 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1653 move(statusoffset,SW);
1654 mutt_paddstr (COLS - 10 - SW, IsHeader (extra) ||
1655 IsMsgAttach (extra) ? buffer : banner);
1658 if (last_pos < sb.st_size - 1)
1659 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1661 addstr (topline == 0 ? "all)" : "end)");
1662 BKGDSET (MT_COLOR_NORMAL);
1663 SETCOLOR (MT_COLOR_NORMAL);
1666 if (redraw & REDRAW_SIDEBAR)
1667 sidebar_draw (MENU_PAGER);
1669 if ((redraw & REDRAW_INDEX) && index) {
1670 /* redraw the pager_index indicator, because the
1671 * flags for this message might have changed. */
1672 menu_redraw_current (index);
1673 sidebar_draw (MENU_PAGER);
1674 /* print out the index status bar */
1675 menu_status_line (buffer, sizeof (buffer), index, NONULL (Status));
1676 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), SW);
1677 SETCOLOR (MT_COLOR_STATUS);
1678 BKGDSET (MT_COLOR_STATUS);
1679 mutt_paddstr (COLS-SW, buffer);
1680 SETCOLOR (MT_COLOR_NORMAL);
1681 BKGDSET (MT_COLOR_NORMAL);
1683 /* if we're not using the index, update every time */
1685 sidebar_draw (MENU_PAGER);
1689 if (option(OPTBRAILLEFRIENDLY)) {
1690 if (brailleLine!=-1) {
1691 move(brailleLine+1, 0);
1695 move (statusoffset, COLS-1);
1698 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1699 && lineInfo[curline].offset < sb.st_size-1) {
1700 if (TopLine - topline > lines)
1709 ch = km_dokey (MENU_PAGER);
1711 mutt_clear_error ();
1718 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1719 else if (SigWinch) {
1720 mutt_resize_screen ();
1722 /* Store current position. */
1724 for (i = 0; i <= topline; i++)
1725 if (!lineInfo[i].continuation)
1728 if (flags & M_PAGER_RETWINCH) {
1729 Resize = p_new(struct resize, 1);
1731 Resize->line = lines;
1732 Resize->SearchCompiled = SearchCompiled;
1733 Resize->SearchBack = SearchBack;
1736 rc = OP_REFORMAT_WINCH;
1739 for (i = 0; i < maxLine; i++) {
1740 lineInfo[i].offset = 0;
1741 lineInfo[i].type = -1;
1742 lineInfo[i].continuation = 0;
1743 lineInfo[i].chunks = 0;
1744 lineInfo[i].search_cnt = -1;
1745 lineInfo[i].quote = NULL;
1747 p_realloc(&lineInfo[i].syntax, 1);
1748 if (SearchCompiled && lineInfo[i].search)
1749 p_delete(&(lineInfo[i].search));
1755 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1760 clearok (stdscr, TRUE); /*force complete redraw */
1764 else if (ch == -1) {
1778 if (lineInfo[curline].offset < sb.st_size - 1) {
1779 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1781 else if (option (OPTPAGERSTOP)) {
1782 /* emulate "less -q" and don't go on to the next message. */
1783 mutt_error _("Bottom of message is shown.");
1786 /* end of the current message, so display the next message. */
1787 rc = OP_MAIN_NEXT_UNDELETED;
1795 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1798 mutt_error _("Top of message is shown.");
1802 if (lineInfo[curline].offset < sb.st_size - 1) {
1805 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1811 mutt_error _("Bottom of message is shown.");
1816 topline = upNLines (1, lineInfo, topline, hideQuoted);
1818 mutt_error _("Top of message is shown.");
1825 mutt_error _("Top of message is shown.");
1830 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1832 mutt_error _("Top of message is shown.");
1836 if (lineInfo[curline].offset < sb.st_size - 1) {
1837 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1839 else if (option (OPTPAGERSTOP)) {
1840 /* emulate "less -q" and don't go on to the next message. */
1841 mutt_error _("Bottom of message is shown.");
1844 /* end of the current message, so display the next message. */
1845 rc = OP_MAIN_NEXT_UNDELETED;
1850 case OP_SEARCH_NEXT:
1851 case OP_SEARCH_OPPOSITE:
1852 if (SearchCompiled) {
1854 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1855 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1856 /* searching forward */
1857 for (i = topline + 1; i < lastLine; i++) {
1858 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1859 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1866 mutt_error _("Not found.");
1869 /* searching backward */
1870 for (i = topline - 1; i >= 0; i--) {
1871 if ((!hideQuoted || (has_types &&
1872 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1873 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1880 mutt_error _("Not found.");
1883 if (lineInfo[topline].search_cnt > 0)
1884 SearchFlag = M_SEARCH;
1888 /* no previous search pattern, so fall through to search */
1891 case OP_SEARCH_REVERSE:
1892 m_strcpy(buffer, sizeof(buffer), searchbuf);
1893 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1894 _("Search: ")), buffer, sizeof (buffer),
1898 if (!strcmp (buffer, searchbuf)) {
1899 if (SearchCompiled) {
1900 /* do an implicit search-next */
1901 if (ch == OP_SEARCH)
1902 ch = OP_SEARCH_NEXT;
1904 ch = OP_SEARCH_OPPOSITE;
1913 m_strcpy(searchbuf, sizeof(searchbuf), buffer);
1915 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1916 if (ch == OP_SEARCH)
1918 else if (ch == OP_SEARCH_REVERSE)
1921 if (SearchCompiled) {
1922 regfree (&SearchRE);
1923 for (i = 0; i < lastLine; i++) {
1924 if (lineInfo[i].search)
1925 p_delete(&(lineInfo[i].search));
1926 lineInfo[i].search_cnt = -1;
1931 REGCOMP (&SearchRE, searchbuf,
1932 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1933 regerror (err, &SearchRE, buffer, sizeof (buffer));
1934 mutt_error ("%s", buffer);
1935 regfree (&SearchRE);
1936 for (i = 0; i < maxLine; i++) {
1938 if (lineInfo[i].search)
1939 p_delete(&(lineInfo[i].search));
1940 lineInfo[i].search_cnt = -1;
1947 /* update the search pointers */
1949 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1950 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1951 &QuoteList, &q_level,
1952 &force_redraw, &SearchRE) == 0) {
1954 redraw |= REDRAW_SIDEBAR;
1958 /* searching forward */
1959 for (i = topline; i < lastLine; i++) {
1960 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1961 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1969 /* searching backward */
1970 for (i = topline; i >= 0; i--) {
1971 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1972 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1980 if (lineInfo[topline].search_cnt == 0) {
1982 mutt_error _("Not found.");
1985 SearchFlag = M_SEARCH;
1987 redraw = REDRAW_BODY;
1990 case OP_SEARCH_TOGGLE:
1991 if (SearchCompiled) {
1992 SearchFlag ^= M_SEARCH;
1993 redraw = REDRAW_BODY;
1998 /* don't let the user enter the help-menu from the help screen! */
2001 mutt_help (MENU_PAGER);
2002 redraw = REDRAW_FULL;
2006 mutt_error _("Help is currently being shown.");
2009 case OP_PAGER_HIDE_QUOTED:
2011 hideQuoted ^= M_HIDE;
2012 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
2013 topline = upNLines (1, lineInfo, topline, hideQuoted);
2015 redraw = REDRAW_BODY;
2019 case OP_PAGER_SKIP_QUOTED:
2022 int new_topline = topline;
2024 while ((new_topline < lastLine ||
2025 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2026 new_topline, &lastLine,
2027 &maxLine, M_TYPES, &QuoteList,
2028 &q_level, &force_redraw,
2030 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
2031 redraw |= REDRAW_SIDEBAR;
2036 mutt_error _("No more quoted text.");
2041 while ((new_topline < lastLine ||
2042 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2043 new_topline, &lastLine,
2044 &maxLine, M_TYPES, &QuoteList,
2045 &q_level, &force_redraw,
2047 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2049 redraw |= REDRAW_SIDEBAR;
2053 mutt_error _("No more unquoted text after quoted text.");
2057 topline = new_topline;
2061 case OP_PAGER_BOTTOM: /* move to the end of the file */
2062 if (lineInfo[curline].offset < sb.st_size - 1) {
2064 /* make sure the types are defined to the end of file */
2065 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2066 &maxLine, has_types,
2067 &QuoteList, &q_level, &force_redraw,
2070 redraw |= REDRAW_SIDEBAR;
2072 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2075 mutt_error _("Bottom of message is shown.");
2079 clearok (stdscr, TRUE);
2080 redraw = REDRAW_FULL;
2084 km_error_key (MENU_PAGER);
2087 /* --------------------------------------------------------------------
2088 * The following are operations on the current message rather than
2089 * adjusting the view of the message.
2092 case OP_BOUNCE_MESSAGE:
2093 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2095 if (IsMsgAttach (extra))
2096 mutt_attach_bounce (extra->fp, extra->hdr,
2097 extra->idx, extra->idxlen, extra->bdy);
2099 ci_bounce_message (extra->hdr, &redraw);
2103 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2105 if (IsMsgAttach (extra))
2106 mutt_attach_resend (extra->fp, extra->hdr,
2107 extra->idx, extra->idxlen, extra->bdy);
2109 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2110 redraw = REDRAW_FULL;
2113 case OP_CHECK_TRADITIONAL:
2114 CHECK_MODE (IsHeader (extra));
2115 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2117 rc = OP_CHECK_TRADITIONAL;
2121 case OP_CREATE_ALIAS:
2122 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2123 if (IsMsgAttach (extra))
2124 mutt_create_alias (extra->bdy->hdr->env, NULL);
2126 mutt_create_alias (extra->hdr->env, NULL);
2127 MAYBE_REDRAW (redraw);
2130 case OP_PURGE_MESSAGE:
2132 CHECK_MODE (IsHeader (extra));
2135 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2137 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2138 mutt_set_flag (Context, extra->hdr, M_PURGED,
2139 ch != OP_PURGE_MESSAGE ? 0 : 1);
2140 if (option (OPTDELETEUNTAG))
2141 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2142 redraw = REDRAW_STATUS | REDRAW_INDEX;
2143 if (option (OPTRESOLVE)) {
2145 rc = OP_MAIN_NEXT_UNDELETED;
2149 case OP_DELETE_THREAD:
2150 case OP_DELETE_SUBTHREAD:
2151 CHECK_MODE (IsHeader (extra));
2154 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2156 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2157 ch == OP_DELETE_THREAD ? 0 : 1);
2160 if (option (OPTDELETEUNTAG))
2161 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2162 ch == OP_DELETE_THREAD ? 0 : 1);
2163 if (option (OPTRESOLVE)) {
2164 rc = OP_MAIN_NEXT_UNDELETED;
2168 if (!option (OPTRESOLVE) && PagerIndexLines)
2169 redraw = REDRAW_FULL;
2171 redraw = REDRAW_STATUS | REDRAW_INDEX;
2175 case OP_DISPLAY_ADDRESS:
2176 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2177 if (IsMsgAttach (extra))
2178 mutt_display_address (extra->bdy->hdr->env);
2180 mutt_display_address (extra->hdr->env);
2183 case OP_ENTER_COMMAND:
2184 old_smart_wrap = option (OPTWRAP);
2185 old_markers = option (OPTMARKERS);
2186 old_PagerIndexLines = PagerIndexLines;
2188 CurrentMenu = MENU_PAGER;
2189 mutt_enter_command ();
2191 if (option (OPTNEEDRESORT)) {
2192 unset_option (OPTNEEDRESORT);
2193 CHECK_MODE (IsHeader (extra));
2194 set_option (OPTNEEDRESORT);
2197 if (old_PagerIndexLines != PagerIndexLines) {
2199 mutt_menuDestroy (&index);
2203 if (option (OPTWRAP) != old_smart_wrap ||
2204 option (OPTMARKERS) != old_markers) {
2205 if (flags & M_PAGER_RETWINCH) {
2207 rc = OP_REFORMAT_WINCH;
2211 /* count the real lines above */
2213 for (i = 0; i <= topline; i++) {
2214 if (!lineInfo[i].continuation)
2218 /* we need to restart the whole thing */
2219 for (i = 0; i < maxLine; i++) {
2220 lineInfo[i].offset = 0;
2221 lineInfo[i].type = -1;
2222 lineInfo[i].continuation = 0;
2223 lineInfo[i].chunks = 0;
2224 lineInfo[i].search_cnt = -1;
2225 lineInfo[i].quote = NULL;
2227 p_realloc(&(lineInfo[i].syntax), 1);
2228 if (SearchCompiled && lineInfo[i].search)
2229 p_delete(&(lineInfo[i].search));
2232 if (SearchCompiled) {
2233 regfree (&SearchRE);
2238 /* try to keep the old position */
2241 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2242 &lastLine, &maxLine,
2243 (has_types ? M_TYPES : 0),
2244 &QuoteList, &q_level, &force_redraw,
2246 redraw |= REDRAW_SIDEBAR;
2247 if (!lineInfo[topline].continuation)
2256 if (option (OPTFORCEREDRAWPAGER))
2257 redraw = REDRAW_FULL;
2258 unset_option (OPTFORCEREDRAWINDEX);
2259 unset_option (OPTFORCEREDRAWPAGER);
2262 case OP_FLAG_MESSAGE:
2263 CHECK_MODE (IsHeader (extra));
2266 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2268 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2269 redraw = REDRAW_STATUS | REDRAW_INDEX;
2270 if (option (OPTRESOLVE)) {
2272 rc = OP_MAIN_NEXT_UNDELETED;
2277 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2278 if (IsAttach (extra))
2279 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2281 mutt_pipe_message (extra->hdr);
2282 MAYBE_REDRAW (redraw);
2286 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2287 if (IsAttach (extra))
2288 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2290 mutt_print_message (extra->hdr);
2294 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2296 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2297 redraw = REDRAW_FULL;
2302 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2304 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2305 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2306 query_quadoption (OPT_TOMODERATED,
2308 ("Posting to this group not allowed, may be moderated. Continue?"))
2311 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2312 redraw = REDRAW_FULL;
2315 case OP_FORWARD_TO_GROUP:
2316 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2318 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2319 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2320 query_quadoption (OPT_TOMODERATED,
2322 ("Posting to this group not allowed, may be moderated. Continue?"))
2325 if (IsMsgAttach (extra))
2326 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2327 extra->idxlen, extra->bdy, SENDNEWS);
2329 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2331 redraw = REDRAW_FULL;
2335 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2338 if (IsMsgAttach (extra))
2339 followup_to = extra->bdy->hdr->env->followup_to;
2341 followup_to = extra->hdr->env->followup_to;
2343 if (!followup_to || m_strcasecmp(followup_to, "poster") ||
2344 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2345 _("Reply by mail as poster prefers?")) != M_YES) {
2346 if (extra->ctx && extra->ctx->magic == M_NNTP
2347 && !((NNTP_DATA *) extra->ctx->data)->allowed
2348 && query_quadoption (OPT_TOMODERATED,
2350 ("Posting to this group not allowed, may be moderated. Continue?"))
2353 if (IsMsgAttach (extra))
2354 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2355 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2357 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2358 extra->ctx, extra->hdr);
2359 redraw = REDRAW_FULL;
2365 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2367 if (IsMsgAttach (extra))
2368 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2369 extra->idxlen, extra->bdy, SENDREPLY);
2371 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2372 redraw = REDRAW_FULL;
2375 case OP_RECALL_MESSAGE:
2376 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2378 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2379 redraw = REDRAW_FULL;
2382 case OP_GROUP_REPLY:
2383 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2385 if (IsMsgAttach (extra))
2386 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2387 extra->idxlen, extra->bdy,
2388 SENDREPLY | SENDGROUPREPLY);
2390 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2392 redraw = REDRAW_FULL;
2396 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2398 if (IsMsgAttach (extra))
2399 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2400 extra->idxlen, extra->bdy,
2401 SENDREPLY | SENDLISTREPLY);
2403 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2405 redraw = REDRAW_FULL;
2408 case OP_FORWARD_MESSAGE:
2409 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2411 if (IsMsgAttach (extra))
2412 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2413 extra->idxlen, extra->bdy, 0);
2415 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2416 redraw = REDRAW_FULL;
2419 case OP_DECRYPT_SAVE:
2421 if (IsAttach (extra)) {
2422 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2427 case OP_COPY_MESSAGE:
2428 case OP_DECODE_SAVE:
2429 case OP_DECODE_COPY:
2430 case OP_DECRYPT_COPY:
2431 CHECK_MODE (IsHeader (extra));
2432 if (mutt_save_message (extra->hdr,
2433 (ch == OP_DECRYPT_SAVE) ||
2434 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2435 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2436 (ch == OP_DECRYPT_SAVE)
2437 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2438 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2439 || ch == OP_DECRYPT_SAVE)) {
2440 if (option (OPTRESOLVE)) {
2442 rc = OP_MAIN_NEXT_UNDELETED;
2445 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2447 MAYBE_REDRAW (redraw);
2450 case OP_SHELL_ESCAPE:
2451 mutt_shell_escape ();
2452 MAYBE_REDRAW (redraw);
2456 CHECK_MODE (IsHeader (extra));
2457 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2459 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2460 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2461 ? NULL : Context->last_tag);
2463 redraw = REDRAW_STATUS | REDRAW_INDEX;
2464 if (option (OPTRESOLVE)) {
2471 CHECK_MODE (IsHeader (extra));
2474 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2476 if (extra->hdr->read || extra->hdr->old)
2477 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2479 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2481 Context->msgnotreadyet = -1;
2482 redraw = REDRAW_STATUS | REDRAW_INDEX;
2483 if (option (OPTRESOLVE)) {
2485 rc = OP_MAIN_NEXT_UNDELETED;
2490 CHECK_MODE (IsHeader (extra));
2493 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2495 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2496 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2497 redraw = REDRAW_STATUS | REDRAW_INDEX;
2498 if (option (OPTRESOLVE)) {
2504 case OP_UNDELETE_THREAD:
2505 case OP_UNDELETE_SUBTHREAD:
2506 CHECK_MODE (IsHeader (extra));
2509 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2511 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2512 ch == OP_UNDELETE_THREAD ? 0 : 1)
2513 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2514 ch == OP_UNDELETE_THREAD ? 0 : 1);
2517 if (option (OPTRESOLVE)) {
2518 rc = (ch == OP_DELETE_THREAD) ?
2519 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2523 if (!option (OPTRESOLVE) && PagerIndexLines)
2524 redraw = REDRAW_FULL;
2526 redraw = REDRAW_STATUS | REDRAW_INDEX;
2535 if (option (OPTFORCEBUFFYCHECK))
2538 redraw |= REDRAW_SIDEBAR;
2541 case OP_VIEW_ATTACHMENTS:
2542 if (flags & M_PAGER_ATTACHMENT) {
2544 rc = OP_ATTACH_COLLAPSE;
2547 CHECK_MODE (IsHeader (extra));
2548 mutt_view_attachments (extra->hdr);
2549 if (extra->hdr->attach_del)
2550 Context->changed = 1;
2551 redraw = REDRAW_FULL;
2556 CHECK_MODE (IsHeader (extra));
2558 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2559 redraw = REDRAW_FULL;
2563 case OP_FORGET_PASSPHRASE:
2564 crypt_forget_passphrase ();
2567 case OP_EXTRACT_KEYS:
2568 CHECK_MODE (IsHeader (extra));
2569 crypt_extract_keys_from_messages (extra->hdr);
2570 redraw = REDRAW_FULL;
2573 case OP_SIDEBAR_SCROLL_UP:
2574 case OP_SIDEBAR_SCROLL_DOWN:
2575 case OP_SIDEBAR_NEXT:
2576 case OP_SIDEBAR_NEXT_NEW:
2577 case OP_SIDEBAR_PREV:
2578 case OP_SIDEBAR_PREV_NEW:
2579 sidebar_scroll (ch, MENU_PAGER);
2588 if (IsHeader (extra)) {
2589 Context->msgnotreadyet = -1;
2594 OldHdr = extra->hdr;
2598 cleanup_quote (&QuoteList);
2600 for (i = 0; i < maxLine; i++) {
2601 p_delete(&(lineInfo[i].syntax));
2602 if (SearchCompiled && lineInfo[i].search)
2603 p_delete(&(lineInfo[i].search));
2605 if (SearchCompiled) {
2606 regfree (&SearchRE);
2609 p_delete(&lineInfo);
2611 mutt_menuDestroy (&index);
2612 return (rc != -1 ? rc : 0);